from header_common import *
from header_presentations import *
from header_mission_templates import *
from ID_meshes import *
from header_operations import *
from header_triggers import *
from module_constants import *
import string

####################################################################################################################
#  Each presentation record contains the following fields:
#  1) Presentation id: used for referencing presentations in other files. The prefix prsnt_ is automatically added before each presentation id.
#  2) Presentation flags. See header_presentations.py for a list of available flags
#  3) Presentation background mesh: See module_meshes.py for a list of available background meshes
#  4) Triggers: Simple triggers that are associated with the presentation
####################################################################################################################

##################################
# Form Ranks kit section
#

formation_order_functions = [
    ("@Dismiss", (call_script, "script_formation_dismiss_key_reaction")),
    ("@Tactical charge", (call_script, "script_formation_tactical_charge_key_reaction")),
    ("@Charge", (call_script, "script_formation_charge_key_reaction", 1, "str_formation_charge")),
    ("@Halt", (call_script, "script_formation_charge_key_reaction", 0, "str_formation_halt")),
    ("@Fall back", (call_script, "script_formation_charge_key_reaction", -1, "str_formation_fall_back")),
    ("@Move forward", (call_script, "script_formation_move_key_reaction", 0, 10, "str_formation_advance")),
    ("@Move backward", (call_script, "script_formation_move_key_reaction", 0, -10, "str_formation_move_back")),
    ("@Move left", (call_script, "script_formation_move_key_reaction", -10, 0, "str_formation_move_left")),
    ("@Move right", (call_script, "script_formation_move_key_reaction", 10, 0, "str_formation_move_right")),
    ("@Turn left", (call_script, "script_formation_turn_key_reaction", 15, "str_formation_turn_left")),
    ("@Turn right", (call_script, "script_formation_turn_key_reaction", -15, "str_formation_turn_right")),
    ("@Stand closer", (call_script, "script_formation_density_key_reaction", -1, "str_formation_stand_closer")),
    ("@Spred out", (call_script, "script_formation_density_key_reaction", 1, "str_formation_spread_out")),
    ("@Toward enemies", (call_script, "script_formation_auto_rotation_key_reaction", 1)),
    ("@Free direction", (call_script, "script_formation_auto_rotation_key_reaction", 0)),
    ]

#
# Form Ranks kit section end
###############################

presentations = [
  ("game_credits",prsntf_read_only,mesh_load_window,[
      (ti_on_presentation_load,
       [(assign, "$g_presentation_credits_obj_1", -1),
        (assign, "$g_presentation_credits_obj_2", -1),
        (assign, "$g_presentation_credits_obj_3", -1),
        (assign, "$g_presentation_credits_obj_4", -1),
        (assign, "$g_presentation_credits_obj_5", -1),
        (assign, "$g_presentation_credits_obj_6", -1),
        (assign, "$g_presentation_credits_obj_7", -1),
        (assign, "$g_presentation_credits_obj_8", -1),
        (assign, "$g_presentation_credits_obj_9", -1),
        (assign, "$g_presentation_credits_obj_10", -1),
        (assign, "$g_presentation_credits_obj_11", -1),
        (assign, "$g_presentation_credits_obj_12", -1),
        (assign, "$g_presentation_credits_obj_1_alpha", 0),
        (assign, "$g_presentation_credits_obj_2_alpha", 0),
        (assign, "$g_presentation_credits_obj_3_alpha", 0),
        (assign, "$g_presentation_credits_obj_4_alpha", 0),
        (assign, "$g_presentation_credits_obj_5_alpha", 0),
        (assign, "$g_presentation_credits_obj_6_alpha", 0),
        (assign, "$g_presentation_credits_obj_7_alpha", 0),
        (assign, "$g_presentation_credits_obj_8_alpha", 0),
        (assign, "$g_presentation_credits_obj_9_alpha", 0),
        ]),
      (ti_on_presentation_run,
       [
        (store_trigger_param_1, ":cur_time"),
        (set_fixed_point_multiplier, 1000),
        (presentation_set_duration, 1000000),
        (try_begin),
          (this_or_next|key_clicked, key_space),
          (this_or_next|key_clicked, key_enter),
          (this_or_next|key_clicked, key_escape),
          (this_or_next|key_clicked, key_back_space),
          (this_or_next|key_clicked, key_left_mouse_button),
          (key_clicked, key_right_mouse_button),
          (presentation_set_duration, 0),
        (try_end),
        (try_begin),
          (lt, "$g_presentation_credits_obj_1", 0),
          (str_store_string, s1, "str_credits_1"),
          (create_text_overlay, "$g_presentation_credits_obj_1", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_1", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_1", 0),
          (position_set_x, pos1, 1500),
          (position_set_y, pos1, 1500),
          (overlay_set_size, "$g_presentation_credits_obj_1", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 375),
          (overlay_set_position, "$g_presentation_credits_obj_1", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_1", 1000, 0xFF),
        (else_try),
          (gt, ":cur_time", 2000),
          (eq, "$g_presentation_credits_obj_1_alpha", 0),
          (assign, "$g_presentation_credits_obj_1_alpha", 1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_1", 1000, 0x00),
        (else_try),
          (gt, ":cur_time", 3500),
          (lt, "$g_presentation_credits_obj_2", 0),
          (str_store_string, s1, "str_credits_2"),
          (create_text_overlay, "$g_presentation_credits_obj_2", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_2", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_2", 0),
          (position_set_x, pos1, 1750),
          (position_set_y, pos1, 1750),
          (overlay_set_size, "$g_presentation_credits_obj_2", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 375),
          (overlay_set_position, "$g_presentation_credits_obj_2", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_2", 1000, 0xFF),
        (else_try),
          (gt, ":cur_time", 5500),
          (eq, "$g_presentation_credits_obj_2_alpha", 0),
          (assign, "$g_presentation_credits_obj_2_alpha", 1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_2", 1000, 0x00),
        (else_try),
          (gt, ":cur_time", 7000),
          (lt, "$g_presentation_credits_obj_3", 0),
          (str_store_string, s1, "str_credits_3"),
          (create_text_overlay, "$g_presentation_credits_obj_3", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_3", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_3", 0),
          (position_set_x, pos1, 1750),
          (position_set_y, pos1, 1750),
          (overlay_set_size, "$g_presentation_credits_obj_3", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 375),
          (overlay_set_position, "$g_presentation_credits_obj_3", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_3", 1000, 0xFF),
        (else_try),
          (gt, ":cur_time", 9000),
          (eq, "$g_presentation_credits_obj_3_alpha", 0),
          (assign, "$g_presentation_credits_obj_3_alpha", 1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_3", 1000, 0),
        (else_try),
          (gt, ":cur_time", 10500),
          (lt, "$g_presentation_credits_obj_4", 0),
          (str_store_string, s1, "str_credits_4"),
          (create_text_overlay, "$g_presentation_credits_obj_4", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_4", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_4", 0),
          (position_set_x, pos1, 1750),
          (position_set_y, pos1, 1750),
          (overlay_set_size, "$g_presentation_credits_obj_4", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 375),
          (overlay_set_position, "$g_presentation_credits_obj_4", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_4", 1000, 0xFF),
        (else_try),
          (gt, ":cur_time", 12500),
          (eq, "$g_presentation_credits_obj_4_alpha", 0),
          (assign, "$g_presentation_credits_obj_4_alpha", 1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_4", 1000, 0),
        (else_try),
          (gt, ":cur_time", 14000),
          (lt, "$g_presentation_credits_obj_5", 0),
          (str_store_string, s1, "str_credits_8"),
          (create_text_overlay, "$g_presentation_credits_obj_5", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_5", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_5", 0),
          (position_set_x, pos1, 1750),
          (position_set_y, pos1, 1750),
          (overlay_set_size, "$g_presentation_credits_obj_5", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 375),
          (overlay_set_position, "$g_presentation_credits_obj_5", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_5", 1000, 0xFF),
        (else_try),
          (gt, ":cur_time", 16000),
          (eq, "$g_presentation_credits_obj_5_alpha", 0),
          (assign, "$g_presentation_credits_obj_5_alpha", 1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_5", 1000, 0),
        (else_try),
          (gt, ":cur_time", 17500),
          (lt, "$g_presentation_credits_obj_6", 0),
          (str_store_string, s1, "str_credits_5"),
          (create_text_overlay, "$g_presentation_credits_obj_6", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_6", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_6", 0),
          (position_set_x, pos1, 1750),
          (position_set_y, pos1, 1750),
          (overlay_set_size, "$g_presentation_credits_obj_6", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 375),
          (overlay_set_position, "$g_presentation_credits_obj_6", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_6", 1000, 0xFF),
        (else_try),
          (gt, ":cur_time", 19500),
          (eq, "$g_presentation_credits_obj_6_alpha", 0),
          (assign, "$g_presentation_credits_obj_6_alpha", 1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_6", 1000, 0),
        (else_try),
          (gt, ":cur_time", 21000),
          (lt, "$g_presentation_credits_obj_7", 0),
          (str_store_string, s1, "str_credits_6"),
          (create_text_overlay, "$g_presentation_credits_obj_7", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_7", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_7", 0),
          (position_set_x, pos1, 1750),
          (position_set_y, pos1, 1750),
          (overlay_set_size, "$g_presentation_credits_obj_7", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 375),
          (overlay_set_position, "$g_presentation_credits_obj_7", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_7", 1000, 0xFF),
        (else_try),
          (gt, ":cur_time", 23000),
          (eq, "$g_presentation_credits_obj_7_alpha", 0),
          (assign, "$g_presentation_credits_obj_7_alpha", 1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_7", 1000, 0),
        (else_try),
          (gt, ":cur_time", 24500),
          (lt, "$g_presentation_credits_obj_8", 0),
          (str_store_string, s1, "str_credits_9"),
          (create_text_overlay, "$g_presentation_credits_obj_8", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_8", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_8", 0),
          (position_set_x, pos1, 1750),
          (position_set_y, pos1, 1750),
          (overlay_set_size, "$g_presentation_credits_obj_8", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 375),
          (overlay_set_position, "$g_presentation_credits_obj_8", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_8", 1000, 0xFF),
        (else_try),
          (gt, ":cur_time", 26500),
          (eq, "$g_presentation_credits_obj_8_alpha", 0),
          (assign, "$g_presentation_credits_obj_8_alpha", 1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_8", 1000, 0),
        (else_try),
          (gt, ":cur_time", 28000),
          (lt, "$g_presentation_credits_obj_9", 0),
          (str_store_string, s1, "str_credits_10"),
          (create_text_overlay, "$g_presentation_credits_obj_9", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_9", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_9", 0),
          (position_set_x, pos1, 750),
          (position_set_y, pos1, 750),
          (overlay_set_size, "$g_presentation_credits_obj_9", pos1),
          (position_set_x, pos1, 250),
          (position_set_y, pos1, 485),
          (overlay_set_position, "$g_presentation_credits_obj_9", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_9", 1000, 0xFF),

          (str_store_string, s1, "str_credits_11"),
          (create_text_overlay, "$g_presentation_credits_obj_10", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_10", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_10", 0),
          (position_set_x, pos1, 750),
          (position_set_y, pos1, 750),
          (overlay_set_size, "$g_presentation_credits_obj_10", pos1),
          (position_set_x, pos1, 750),
          (position_set_y, pos1, 470),
          (overlay_set_position, "$g_presentation_credits_obj_10", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_10", 1000, 0xFF),

          (str_store_string, s1, "str_credits_12"),
          (create_text_overlay, "$g_presentation_credits_obj_11", s1, tf_center_justify|tf_double_space|tf_vertical_align_center),
          (overlay_set_color, "$g_presentation_credits_obj_11", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_11", 0),
          (position_set_x, pos1, 750),
          (position_set_y, pos1, 750),
          (overlay_set_size, "$g_presentation_credits_obj_11", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 105),
          (overlay_set_position, "$g_presentation_credits_obj_11", pos1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_11", 1000, 0xFF),
        (else_try),
          (gt, ":cur_time", 34000),
          (eq, "$g_presentation_credits_obj_9_alpha", 0),
          (assign, "$g_presentation_credits_obj_9_alpha", 1),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_9", 1000, 0),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_10", 1000, 0),
          (overlay_animate_to_alpha, "$g_presentation_credits_obj_11", 1000, 0),
        (else_try),
          (gt, ":cur_time", 35500),
          (lt, "$g_presentation_credits_obj_12", 0),
          (str_store_string, s1, "str_credits_7"),
          (create_text_overlay, "$g_presentation_credits_obj_12", s1, tf_center_justify|tf_double_space),
          (overlay_set_color, "$g_presentation_credits_obj_12", 0),
          (overlay_set_alpha, "$g_presentation_credits_obj_12", 0xFF),
          (position_set_x, pos1, 1000),
          (position_set_y, pos1, 1000),
          (overlay_set_size, "$g_presentation_credits_obj_12", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, -6100),
          (overlay_set_position, "$g_presentation_credits_obj_12", pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 760),
          (overlay_animate_to_position, "$g_presentation_credits_obj_12", 70000, pos1),
        (else_try),
          (gt, ":cur_time", 105500),
          (presentation_set_duration, 0),
        (try_end),
        ]),
      ]),
  
  ("banner_selection",0,mesh_load_window,[
      (ti_on_presentation_load,
       [(set_fixed_point_multiplier, 1000),
        (str_store_string, s1, "str_banner_selection_text"),
        (create_text_overlay, reg1, s1, tf_center_justify),
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 600),
        (overlay_set_position, reg1, pos1),
        (overlay_set_text, reg1, s1),
        (create_button_overlay, "$g_presentation_obj_1", "@Next Page", tf_center_justify),
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 50),
        (overlay_set_position, "$g_presentation_obj_1", pos1),

        (assign, ":x_pos", 150),
        (assign, ":y_pos", 575),
        (assign, ":try_end", banner_meshes_end_minus_one),
        (store_mul, ":begin_mesh", 16, "$g_presentation_page_no"),
        (val_add, ":begin_mesh", banner_meshes_begin),
        (store_add, ":try_end_2", ":begin_mesh", 16),
        (val_min, ":try_end", ":try_end_2"),
        (store_add, "$g_presentation_banner_start", "$g_presentation_obj_1", 1),
        (try_for_range, ":cur_banner_mesh", ":begin_mesh", ":try_end"),
          (create_image_button_overlay, reg1, ":cur_banner_mesh", ":cur_banner_mesh"),
          (position_set_x, pos1, ":x_pos"),
          (position_set_y, pos1, ":y_pos"),
          (overlay_set_position, reg1, pos1),
          (position_set_x, pos1, 100),
          (position_set_y, pos1, 100),
          (overlay_set_size, reg1, pos1),
          (val_add, ":x_pos", 100),
          (ge, ":x_pos", 900),
          (assign, ":x_pos", 150),
          (val_sub, ":y_pos", 250),
        (try_end),
        (presentation_set_duration, 999999),
        ]),
      (ti_on_presentation_event_state_change,
       [(store_trigger_param_1, ":object"),
        (try_begin),
          (eq, ":object", "$g_presentation_obj_1"),
          (val_add, "$g_presentation_page_no", 1),
          (val_mod, "$g_presentation_page_no", 8),
          (start_presentation, "prsnt_banner_selection"),
        (else_try),
          (store_sub, ":selected_banner", ":object", "$g_presentation_banner_start"),
          (store_mul, ":page_adder", 16, "$g_presentation_page_no"),
          (val_add, ":selected_banner", ":page_adder"),
          (store_add, ":selected_banner_map_icon", ":selected_banner", banner_map_icons_begin),
          (party_set_banner_icon, "p_main_party", ":selected_banner_map_icon"),
          (store_add, ":selected_banner_spr", ":selected_banner", banner_scene_props_begin),
          (troop_set_slot, "trp_player", slot_troop_banner_scene_prop, ":selected_banner_spr"),
          (presentation_set_duration, 0),
          (assign, ":troop_to_change", 0),
          (assign, ":end_cond", kingdom_heroes_end),
          (try_for_range, ":cur_troop", kingdom_heroes_begin, ":end_cond"),
            (troop_slot_eq, ":cur_troop", slot_troop_banner_scene_prop, ":selected_banner_spr"),
            (assign, ":troop_to_change", ":cur_troop"),
            (assign, ":end_cond", 0),
            (troop_set_slot, ":cur_troop", slot_troop_banner_scene_prop, banner_scene_props_end_minus_one),
            (troop_get_slot, ":cur_party", ":cur_troop", slot_troop_leaded_party),
            (gt, ":cur_party", 0),
            (party_set_banner_icon, ":cur_party", banner_map_icons_end_minus_one),
          (try_end),
          (try_for_range, ":cur_center", walled_centers_begin, walled_centers_end),
            (try_begin),
              (party_slot_eq, ":cur_center", slot_town_lord, "trp_player"),
              (party_set_banner_icon, ":cur_center", ":selected_banner_map_icon"),
            (else_try),
              (party_slot_eq, ":cur_center", slot_town_lord, ":troop_to_change"),
              (party_set_banner_icon, ":cur_center", banner_map_icons_end_minus_one),
            (try_end),
          (try_end),
        (try_end),
        ]),
      (ti_on_presentation_run,
       [(try_begin),
          (this_or_next|key_clicked, key_space),
          (this_or_next|key_clicked, key_enter),
          (this_or_next|key_clicked, key_escape),
          (key_clicked, key_back_space),
          (presentation_set_duration, 0),
        (try_end),

        ]),
      ]),

  ("custom_banner", 0, mesh_load_window,
   [
     (ti_on_presentation_load,
      [
        (set_fixed_point_multiplier, 1000),
        (troop_get_slot, ":flag_type", "trp_player", slot_troop_custom_banner_flag_type),
        (val_max, ":flag_type", 0),
        (val_add, ":flag_type", custom_banner_flag_types_begin),
        (troop_get_slot, ":map_flag_type", "trp_player", slot_troop_custom_banner_map_flag_type),
        (val_max, ":map_flag_type", 0),
        (val_add, ":map_flag_type", custom_banner_flag_map_types_begin),
##        (create_mesh_overlay_with_tableau_material, "$g_presentation_obj_1", ":flag_type", "tableau_custom_banner_square", "trp_player"),
        (troop_get_slot, ":bg_color_1", "trp_player", slot_troop_custom_banner_bg_color_1),
        (troop_get_slot, ":bg_color_2", "trp_player", slot_troop_custom_banner_bg_color_2),
        
        (create_text_overlay, reg1, "str_color_no_1", 0),
        (position_set_x, pos1, 40),
        (position_set_y, pos1, 550),
        (overlay_set_position, reg1, pos1),
        (create_text_overlay, reg1, "str_color_no_2", 0),
        (position_set_x, pos1, 40),
        (position_set_y, pos1, 500),
        (overlay_set_position, reg1, pos1),

        (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_22", "mesh_color_picker", "tableau_color_picker", ":bg_color_1"),
        (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_23", "mesh_color_picker", "tableau_color_picker", ":bg_color_2"),
        (position_set_x, pos1, 200),
        (position_set_y, pos1, 565),
        (overlay_set_position, "$g_presentation_obj_22", pos1),
        (position_set_y, pos1, 515),
        (overlay_set_position, "$g_presentation_obj_23", pos1),
        (position_set_x, pos1, 250),
        (position_set_y, pos1, 250),
        (overlay_set_size, "$g_presentation_obj_22", pos1),
        (overlay_set_size, "$g_presentation_obj_23", pos1),
        
        (create_button_overlay, "$g_presentation_obj_2", "str_change", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_3", "str_change", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_4", "str_change_background", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_5", "str_change", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_19", "str_change", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_20", "str_accept", tf_center_justify),
        (troop_get_slot, ":num_charges", "trp_player", slot_troop_custom_banner_num_charges),
        (create_button_overlay, "$g_presentation_obj_16", "str_randomize", tf_center_justify),
        (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_17", ":flag_type", "tableau_custom_banner_default", "trp_player"),
        (try_begin),
          (eq, ":map_flag_type", "mesh_custom_map_banner_01"),
          (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_18", ":map_flag_type", "tableau_custom_banner_square", "trp_player"),
        (else_try),
          (eq, ":map_flag_type", "mesh_custom_map_banner_02"),
          (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_18", ":map_flag_type", "tableau_custom_banner_short", "trp_player"),
        (else_try),
          (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_18", ":map_flag_type", "tableau_custom_banner_tall", "trp_player"),
        (try_end),
        (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_1", -1, "tableau_custom_banner_square_no_mesh", "trp_player"),
        (create_text_overlay, reg1, "str_sample_banner", tf_center_justify),
        (position_set_x, pos1, 825),
        (position_set_y, pos1, 650),
        (overlay_set_position, reg1, pos1),
        (create_text_overlay, reg1, "str_sample_map_banner", tf_center_justify),
        (position_set_y, pos1, 500),
        (overlay_set_position, reg1, pos1),

        (position_set_x, pos1, 800),
        (position_set_y, pos1, 640),
        (overlay_set_position, "$g_presentation_obj_17", pos1),
        (position_set_x, pos1, 780),
        (position_set_y, pos1, 315),
        (overlay_set_position, "$g_presentation_obj_18", pos1),
        (position_set_x, pos1, 50),
        (position_set_y, pos1, 50),
        (overlay_set_size, "$g_presentation_obj_17", pos1),
        (position_set_x, pos1, 50),
        (position_set_y, pos1, 50),
        (overlay_set_size, "$g_presentation_obj_18", pos1),
        

        (create_text_overlay, reg1, "str_number_of_charges", 0),
        (position_set_x, pos1, 40),
        (position_set_y, pos1, 350),
        (overlay_set_position, reg1, pos1),

        (assign, reg1, ":num_charges"),
        (create_text_overlay, reg2, "str_reg1", tf_center_justify),
        (position_set_x, pos1, 350),
        (position_set_y, pos1, 350),
        (overlay_set_position, reg2, pos1),
        (try_begin),
          (lt, ":num_charges", 4),
          (create_button_overlay, "$g_presentation_obj_15", "str_plus", tf_center_justify),
          (position_set_x, pos1, 385),
          (position_set_y, pos1, 350),
          (overlay_set_position, "$g_presentation_obj_15", pos1),
        (else_try),
          (assign, "$g_presentation_obj_15", -1),
        (try_end),
        (try_begin),
          (gt, ":num_charges", 0),
          (create_button_overlay, "$g_presentation_obj_21", "str_minus", tf_center_justify),
          (position_set_x, pos1, 370),
          (position_set_y, pos1, 350),
          (overlay_set_position, "$g_presentation_obj_21", pos1),
        (else_try),
          (assign, "$g_presentation_obj_21", -1),
        (try_end),

        (try_begin),
          (ge, ":num_charges", 1),
          (create_text_overlay, reg1, "str_charge", tf_center_justify),
          (position_set_x, pos1, 300),
          (position_set_y, pos1, 300),
          (overlay_set_position, reg1, pos1),
          (create_text_overlay, reg1, "str_color", tf_center_justify),
          (position_set_x, pos1, 550),
          (position_set_y, pos1, 300),
          (overlay_set_position, reg1, pos1),
          (create_button_overlay, "$g_presentation_obj_14", "str_change_charge_position", tf_center_justify),
          (position_set_y, pos1, 350),
          (position_set_x, pos1, 550),
          (overlay_set_position, "$g_presentation_obj_14", pos1),

          (create_text_overlay, reg1, "str_charge_no_1", 0),
          (position_set_x, pos1, 40),
          (position_set_y, pos1, 240),
          (overlay_set_position, reg1, pos1),
          (create_button_overlay, "$g_presentation_obj_6", "str_change", 0),
          (position_set_x, pos1, 310),
          (overlay_set_position, "$g_presentation_obj_6", pos1),
          (create_button_overlay, "$g_presentation_obj_7", "str_change", 0),
          (position_set_x, pos1, 560),
          (overlay_set_position, "$g_presentation_obj_7", pos1),
          (troop_get_slot, ":cur_charge", "trp_player", slot_troop_custom_banner_charge_type_1),
          (val_mod, ":cur_charge", 256),
          (val_add, ":cur_charge", custom_banner_charges_begin),
          (create_image_button_overlay, "$g_presentation_obj_24", ":cur_charge", ":cur_charge"),
          (position_set_x, pos1, 260),
          (position_set_y, pos1, 255),
          (overlay_set_position, "$g_presentation_obj_24", pos1),
          (position_set_x, pos1, 50),
          (position_set_y, pos1, 50),
          (overlay_set_size, "$g_presentation_obj_24", pos1),
          (troop_get_slot, ":cur_charge_color", "trp_player", slot_troop_custom_banner_charge_color_1),
          (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_28", "mesh_color_picker", "tableau_color_picker", ":cur_charge_color"),
          (position_set_x, pos1, 530),
          (position_set_y, pos1, 255),
          (overlay_set_position, "$g_presentation_obj_28", pos1),
          (position_set_x, pos1, 250),
          (position_set_y, pos1, 250),
          (overlay_set_size, "$g_presentation_obj_28", pos1),

          (create_button_overlay, "$g_presentation_obj_32", "str_flip_horizontal", 0),
          (position_set_x, pos1, 700),
          (position_set_y, pos1, 240),
          (overlay_set_position, "$g_presentation_obj_32", pos1),
          (create_button_overlay, "$g_presentation_obj_33", "str_flip_vertical", 0),
          (position_set_x, pos1, 800),
          (overlay_set_position, "$g_presentation_obj_33", pos1),
        (try_end),
        (try_begin),
          (ge, ":num_charges", 2),
          (create_text_overlay, reg1, "str_charge_no_2", 0),
          (position_set_x, pos1, 40),
          (position_set_y, pos1, 180),
          (overlay_set_position, reg1, pos1),
          (create_button_overlay, "$g_presentation_obj_8", "str_change", 0),
          (position_set_x, pos1, 310),
          (overlay_set_position, "$g_presentation_obj_8", pos1),
          (create_button_overlay, "$g_presentation_obj_9", "str_change", 0),
          (position_set_x, pos1, 560),
          (overlay_set_position, "$g_presentation_obj_9", pos1),
          (troop_get_slot, ":cur_charge", "trp_player", slot_troop_custom_banner_charge_type_2),
          (val_mod, ":cur_charge", 256),
          (val_add, ":cur_charge", custom_banner_charges_begin),
          (create_image_button_overlay, "$g_presentation_obj_25", ":cur_charge", ":cur_charge"),
          (position_set_x, pos1, 260),
          (position_set_y, pos1, 195),
          (overlay_set_position, "$g_presentation_obj_25", pos1),
          (position_set_x, pos1, 50),
          (position_set_y, pos1, 50),
          (overlay_set_size, "$g_presentation_obj_25", pos1),
          (troop_get_slot, ":cur_charge_color", "trp_player", slot_troop_custom_banner_charge_color_2),
          (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_29", "mesh_color_picker", "tableau_color_picker", ":cur_charge_color"),
          (position_set_x, pos1, 530),
          (position_set_y, pos1, 195),
          (overlay_set_position, "$g_presentation_obj_29", pos1),
          (position_set_x, pos1, 250),
          (position_set_y, pos1, 250),
          (overlay_set_size, "$g_presentation_obj_29", pos1),

          (create_button_overlay, "$g_presentation_obj_34", "str_flip_horizontal", 0),
          (position_set_x, pos1, 700),
          (position_set_y, pos1, 180),
          (overlay_set_position, "$g_presentation_obj_34", pos1),
          (create_button_overlay, "$g_presentation_obj_35", "str_flip_vertical", 0),
          (position_set_x, pos1, 800),
          (overlay_set_position, "$g_presentation_obj_35", pos1),
        (try_end),
        (try_begin),
          (ge, ":num_charges", 3),
          (create_text_overlay, reg1, "str_charge_no_3", 0),
          (position_set_x, pos1, 40),
          (position_set_y, pos1, 120),
          (overlay_set_position, reg1, pos1),
          (create_button_overlay, "$g_presentation_obj_10", "str_change", 0),
          (position_set_x, pos1, 310),
          (overlay_set_position, "$g_presentation_obj_10", pos1),
          (create_button_overlay, "$g_presentation_obj_11", "str_change", 0),
          (position_set_x, pos1, 560),
          (overlay_set_position, "$g_presentation_obj_11", pos1),
          (troop_get_slot, ":cur_charge", "trp_player", slot_troop_custom_banner_charge_type_3),
          (val_mod, ":cur_charge", 256),
          (val_add, ":cur_charge", custom_banner_charges_begin),
          (create_image_button_overlay, "$g_presentation_obj_26", ":cur_charge", ":cur_charge"),
          (position_set_x, pos1, 260),
          (position_set_y, pos1, 135),
          (overlay_set_position, "$g_presentation_obj_26", pos1),
          (position_set_x, pos1, 50),
          (position_set_y, pos1, 50),
          (overlay_set_size, "$g_presentation_obj_26", pos1),
          (troop_get_slot, ":cur_charge_color", "trp_player", slot_troop_custom_banner_charge_color_3),
          (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_30", "mesh_color_picker", "tableau_color_picker", ":cur_charge_color"),
          (position_set_x, pos1, 530),
          (position_set_y, pos1, 135),
          (overlay_set_position, "$g_presentation_obj_30", pos1),
          (position_set_x, pos1, 250),
          (position_set_y, pos1, 250),
          (overlay_set_size, "$g_presentation_obj_30", pos1),

          (create_button_overlay, "$g_presentation_obj_36", "str_flip_horizontal", 0),
          (position_set_x, pos1, 700),
          (position_set_y, pos1, 120),
          (overlay_set_position, "$g_presentation_obj_36", pos1),
          (create_button_overlay, "$g_presentation_obj_37", "str_flip_vertical", 0),
          (position_set_x, pos1, 800),
          (overlay_set_position, "$g_presentation_obj_37", pos1),
        (try_end),
        (try_begin),
          (ge, ":num_charges", 4),
          (create_text_overlay, reg1, "str_charge_no_4", 0),
          (position_set_x, pos1, 40),
          (position_set_y, pos1, 60),
          (overlay_set_position, reg1, pos1),
          (create_button_overlay, "$g_presentation_obj_12", "str_change", 0),
          (position_set_x, pos1, 310),
          (overlay_set_position, "$g_presentation_obj_12", pos1),
          (create_button_overlay, "$g_presentation_obj_13", "str_change", 0),
          (position_set_x, pos1, 560),
          (overlay_set_position, "$g_presentation_obj_13", pos1),
          (troop_get_slot, ":cur_charge", "trp_player", slot_troop_custom_banner_charge_type_4),
          (val_mod, ":cur_charge", 256),
          (val_add, ":cur_charge", custom_banner_charges_begin),
          (create_image_button_overlay, "$g_presentation_obj_27", ":cur_charge", ":cur_charge"),
          (position_set_x, pos1, 260),
          (position_set_y, pos1, 75),
          (overlay_set_position, "$g_presentation_obj_27", pos1),
          (position_set_x, pos1, 50),
          (position_set_y, pos1, 50),
          (overlay_set_size, "$g_presentation_obj_27", pos1),
          (troop_get_slot, ":cur_charge_color", "trp_player", slot_troop_custom_banner_charge_color_4),
          (create_image_button_overlay_with_tableau_material, "$g_presentation_obj_31", "mesh_color_picker", "tableau_color_picker", ":cur_charge_color"),
          (position_set_x, pos1, 530),
          (position_set_y, pos1, 75),
          (overlay_set_position, "$g_presentation_obj_31", pos1),
          (position_set_x, pos1, 250),
          (position_set_y, pos1, 250),
          (overlay_set_size, "$g_presentation_obj_31", pos1),

          (create_button_overlay, "$g_presentation_obj_38", "str_flip_horizontal", 0),
          (position_set_x, pos1, 700),
          (position_set_y, pos1, 60),
          (overlay_set_position, "$g_presentation_obj_38", pos1),
          (create_button_overlay, "$g_presentation_obj_39", "str_flip_vertical", 0),
          (position_set_x, pos1, 800),
          (overlay_set_position, "$g_presentation_obj_39", pos1),
        (try_end),
        
        (position_set_x, pos1, 350),
        (position_set_y, pos1, 400),
        (overlay_set_position, "$g_presentation_obj_1", pos1),
        (position_set_x, pos1, 275),
        (position_set_y, pos1, 550),
        (overlay_set_position, "$g_presentation_obj_2", pos1),
        (position_set_y, pos1, 500),
        (overlay_set_position, "$g_presentation_obj_3", pos1),
        (position_set_x, pos1, 175),
        (position_set_y, pos1, 600),
        (overlay_set_position, "$g_presentation_obj_4", pos1),
        (position_set_x, pos1, 880),
        (position_set_y, pos1, 575),
        (overlay_set_position, "$g_presentation_obj_5", pos1),
        (position_set_y, pos1, 400),
        (overlay_set_position, "$g_presentation_obj_19", pos1),
        (position_set_y, pos1, 650),
        (position_set_x, pos1, 175),
        (overlay_set_position, "$g_presentation_obj_16", pos1),
        (position_set_y, pos1, 150),
        (position_set_x, pos1, 850),
        (overlay_set_position, "$g_presentation_obj_20", pos1),
        (presentation_set_duration, 999999),
        ]),
     (ti_on_presentation_event_state_change,
      [
        (store_trigger_param_1, ":object"),
        (troop_get_slot, ":num_charges", "trp_player", slot_troop_custom_banner_num_charges),
        (try_begin),
          (this_or_next|eq, ":object", "$g_presentation_obj_2"),
          (eq, ":object", "$g_presentation_obj_22"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (assign, "$g_presentation_output_slot", slot_troop_custom_banner_bg_color_1),
          (start_presentation, "prsnt_color_selection"),
        (else_try),
          (this_or_next|eq, ":object", "$g_presentation_obj_3"),
          (eq, ":object", "$g_presentation_obj_23"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (assign, "$g_presentation_output_slot", slot_troop_custom_banner_bg_color_2),
          (start_presentation, "prsnt_color_selection"),
        (else_try),
          (this_or_next|eq, ":object", "$g_presentation_obj_4"),
          (eq, ":object", "$g_presentation_obj_1"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (start_presentation, "prsnt_banner_background_selection"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_5"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (start_presentation, "prsnt_banner_flag_type_selection"),
        (else_try),
          (ge, ":num_charges", 1),
          (this_or_next|eq, ":object", "$g_presentation_obj_6"),
          (eq, ":object", "$g_presentation_obj_24"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (assign, "$g_presentation_output_slot", slot_troop_custom_banner_charge_type_1),
          (start_presentation, "prsnt_banner_charge_selection"),
        (else_try),
          (ge, ":num_charges", 1),
          (this_or_next|eq, ":object", "$g_presentation_obj_7"),
          (eq, ":object", "$g_presentation_obj_28"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (assign, "$g_presentation_output_slot", slot_troop_custom_banner_charge_color_1),
          (start_presentation, "prsnt_color_selection"),
        (else_try),
          (ge, ":num_charges", 2),
          (this_or_next|eq, ":object", "$g_presentation_obj_8"),
          (eq, ":object", "$g_presentation_obj_25"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (assign, "$g_presentation_output_slot", slot_troop_custom_banner_charge_type_2),
          (start_presentation, "prsnt_banner_charge_selection"),
        (else_try),
          (ge, ":num_charges", 2),
          (this_or_next|eq, ":object", "$g_presentation_obj_9"),
          (eq, ":object", "$g_presentation_obj_29"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (assign, "$g_presentation_output_slot", slot_troop_custom_banner_charge_color_2),
          (start_presentation, "prsnt_color_selection"),
        (else_try),
          (ge, ":num_charges", 3),
          (this_or_next|eq, ":object", "$g_presentation_obj_10"),
          (eq, ":object", "$g_presentation_obj_26"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (assign, "$g_presentation_output_slot", slot_troop_custom_banner_charge_type_3),
          (start_presentation, "prsnt_banner_charge_selection"),
        (else_try),
          (ge, ":num_charges", 3),
          (this_or_next|eq, ":object", "$g_presentation_obj_11"),
          (eq, ":object", "$g_presentation_obj_30"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (assign, "$g_presentation_output_slot", slot_troop_custom_banner_charge_color_3),
          (start_presentation, "prsnt_color_selection"),
        (else_try),
          (ge, ":num_charges", 4),
          (this_or_next|eq, ":object", "$g_presentation_obj_12"),
          (eq, ":object", "$g_presentation_obj_27"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (assign, "$g_presentation_output_slot", slot_troop_custom_banner_charge_type_4),
          (start_presentation, "prsnt_banner_charge_selection"),
        (else_try),
          (ge, ":num_charges", 4),
          (this_or_next|eq, ":object", "$g_presentation_obj_13"),
          (eq, ":object", "$g_presentation_obj_31"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (assign, "$g_presentation_output_slot", slot_troop_custom_banner_charge_color_4),
          (start_presentation, "prsnt_color_selection"),
        (else_try),
          (ge, ":num_charges", 1),
          (eq, ":object", "$g_presentation_obj_14"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (start_presentation, "prsnt_banner_charge_positioning"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_15"),
          (troop_get_slot, ":num_charges", "trp_player", slot_troop_custom_banner_num_charges),
          (val_add, ":num_charges", 1),
          (val_clamp, ":num_charges", 0, 5),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_num_charges, ":num_charges"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_21"),
          (troop_get_slot, ":num_charges", "trp_player", slot_troop_custom_banner_num_charges),
          (val_sub, ":num_charges", 1),
          (val_clamp, ":num_charges", 0, 5),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_num_charges, ":num_charges"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_16"),
          (call_script, "script_get_random_custom_banner", "trp_player"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_17"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (start_presentation, "prsnt_banner_flag_type_selection"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_18"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (start_presentation, "prsnt_banner_flag_map_type_selection"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_19"),
          (assign, "$g_presentation_next_presentation", "prsnt_custom_banner"),
          (start_presentation, "prsnt_banner_flag_map_type_selection"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_32"),
          (troop_get_slot, ":charge_type", "trp_player", slot_troop_custom_banner_charge_type_1),
          (store_mod, ":charge_type_base", ":charge_type", 256),
          (val_div, ":charge_type", 256),
          (store_mod, ":flag", ":charge_type", 2),
          (val_div, ":charge_type", 2), #remove flag
          (val_add, ":flag", 1),
          (val_mod, ":flag", 2), #switch flag
          (val_mul, ":charge_type", 2),
          (val_add, ":charge_type", ":flag"),
          (val_mul, ":charge_type", 256),
          (val_add, ":charge_type", ":charge_type_base"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_type_1, ":charge_type"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_34"),
          (troop_get_slot, ":charge_type", "trp_player", slot_troop_custom_banner_charge_type_2),
          (store_mod, ":charge_type_base", ":charge_type", 256),
          (val_div, ":charge_type", 256),
          (store_mod, ":flag", ":charge_type", 2),
          (val_div, ":charge_type", 2), #remove flag
          (val_add, ":flag", 1),
          (val_mod, ":flag", 2), #switch flag
          (val_mul, ":charge_type", 2),
          (val_add, ":charge_type", ":flag"),
          (val_mul, ":charge_type", 256),
          (val_add, ":charge_type", ":charge_type_base"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_type_2, ":charge_type"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_36"),
          (troop_get_slot, ":charge_type", "trp_player", slot_troop_custom_banner_charge_type_3),
          (store_mod, ":charge_type_base", ":charge_type", 256),
          (val_div, ":charge_type", 256),
          (store_mod, ":flag", ":charge_type", 2),
          (val_div, ":charge_type", 2), #remove flag
          (val_add, ":flag", 1),
          (val_mod, ":flag", 2), #switch flag
          (val_mul, ":charge_type", 2),
          (val_add, ":charge_type", ":flag"),
          (val_mul, ":charge_type", 256),
          (val_add, ":charge_type", ":charge_type_base"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_type_3, ":charge_type"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_38"),
          (troop_get_slot, ":charge_type", "trp_player", slot_troop_custom_banner_charge_type_4),
          (store_mod, ":charge_type_base", ":charge_type", 256),
          (val_div, ":charge_type", 256),
          (store_mod, ":flag", ":charge_type", 2),
          (val_div, ":charge_type", 2), #remove flag
          (val_add, ":flag", 1),
          (val_mod, ":flag", 2), #switch flag
          (val_mul, ":charge_type", 2),
          (val_add, ":charge_type", ":flag"),
          (val_mul, ":charge_type", 256),
          (val_add, ":charge_type", ":charge_type_base"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_type_4, ":charge_type"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_33"),
          (troop_get_slot, ":charge_type", "trp_player", slot_troop_custom_banner_charge_type_1),
          (store_div, ":flag", ":charge_type", 512),
          (val_add, ":flag", 1),
          (val_mod, ":flag", 2), #switch flag
          (val_mul, ":flag", 512),
          (val_mod, ":charge_type", 512), #remove flag
          (val_add, ":charge_type", ":flag"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_type_1, ":charge_type"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_35"),
          (troop_get_slot, ":charge_type", "trp_player", slot_troop_custom_banner_charge_type_2),
          (store_div, ":flag", ":charge_type", 512),
          (val_add, ":flag", 1),
          (val_mod, ":flag", 2), #switch flag
          (val_mul, ":flag", 512),
          (val_mod, ":charge_type", 512), #remove flag
          (val_add, ":charge_type", ":flag"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_type_2, ":charge_type"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_37"),
          (troop_get_slot, ":charge_type", "trp_player", slot_troop_custom_banner_charge_type_3),
          (store_div, ":flag", ":charge_type", 512),
          (val_add, ":flag", 1),
          (val_mod, ":flag", 2), #switch flag
          (val_mul, ":flag", 512),
          (val_mod, ":charge_type", 512), #remove flag
          (val_add, ":charge_type", ":flag"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_type_3, ":charge_type"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_39"),
          (troop_get_slot, ":charge_type", "trp_player", slot_troop_custom_banner_charge_type_4),
          (store_div, ":flag", ":charge_type", 512),
          (val_add, ":flag", 1),
          (val_mod, ":flag", 2), #switch flag
          (val_mul, ":flag", 512),
          (val_mod, ":charge_type", 512), #remove flag
          (val_add, ":charge_type", ":flag"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_charge_type_4, ":charge_type"),
          (start_presentation, "prsnt_custom_banner"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_20"),
          (troop_get_slot, ":flag_icon", "trp_player", slot_troop_custom_banner_map_flag_type),
          (try_begin),
            (ge, ":flag_icon", 0),
            (val_add, ":flag_icon", custom_banner_map_icons_begin),
            (party_set_banner_icon, "p_main_party", ":flag_icon"),
          (try_end),
          (presentation_set_duration, 0),
        (try_end),
        ]),
##     (ti_on_presentation_run,
##      [(try_begin),
##         (this_or_next|key_clicked, key_space),
##         (this_or_next|key_clicked, key_enter),
##         (this_or_next|key_clicked, key_escape),
##         (key_clicked, key_back_space),
##         (presentation_set_duration, 0),
##       (try_end),
##       ]),
     ]),

  ("banner_charge_positioning", 0, mesh_load_window,
   [
     (ti_on_presentation_load,
      [
        (set_fixed_point_multiplier, 1000),
        (create_text_overlay, reg1, "str_choose_position", tf_center_justify),
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 600),
        (overlay_set_position, reg1, pos1),
        (assign, ":pos_x", 125),
        (assign, ":pos_y", 400),
        (call_script, "script_get_troop_custom_banner_num_positionings", "trp_player"),
        (assign, ":num_positionings", reg0),
        (store_mul, ":x_needed", ":num_positionings", 125),
        (val_sub, ":x_needed", 25),
        (store_div, ":pos_x", ":x_needed", 2),
        (store_sub, ":pos_x", 500, ":pos_x"),
        (try_for_range, ":cur_positioning", 0, ":num_positionings"),
          (create_image_button_overlay_with_tableau_material, reg1, -1, "tableau_positioning_selection", ":cur_positioning"),
          (position_set_x, pos1, ":pos_x"),
          (position_set_y, pos1, ":pos_y"),
          (val_add, ":pos_x", 125),
          (overlay_set_position, reg1, pos1),
          (troop_set_slot, "trp_temp_array_a", ":cur_positioning", reg1),
        (try_end),
        (presentation_set_duration, 999999),
        ]),
     (ti_on_presentation_event_state_change,
      [
        (store_trigger_param_1, ":object"),
        (call_script, "script_get_troop_custom_banner_num_positionings", "trp_player"),
        (assign, ":num_positionings", reg0),
        (try_for_range, ":trp_slot_index", 0, ":num_positionings"),
          (troop_slot_eq, "trp_temp_array_a", ":trp_slot_index", ":object"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_positioning, ":trp_slot_index"),
          (assign, ":num_positionings", 0),
        (try_end),
        (try_begin),
          (gt, "$g_presentation_next_presentation", 0),
          (start_presentation, "$g_presentation_next_presentation"),
        (else_try),
          (presentation_set_duration, 0),
        (try_end),
        ]),
     ]),

  ("banner_charge_selection", 0, mesh_load_window,
   [
     (ti_on_presentation_load,
      [
        (set_fixed_point_multiplier, 1000),
        (create_text_overlay, reg1, "str_choose_charge", tf_center_justify),

        (position_set_x, pos1, 500),
        (position_set_y, pos1, 650),
        (overlay_set_position, reg1, pos1),
        (assign, ":pos_x", 100),
        (assign, ":pos_y", 600),
        (try_for_range, ":cur_charge", custom_banner_charges_begin, custom_banner_charges_end),
          (create_image_button_overlay, reg1, ":cur_charge", ":cur_charge"),
          (position_set_x, pos1, ":pos_x"),
          (position_set_y, pos1, ":pos_y"),
          (val_add, ":pos_x", 100),
          (try_begin),
            (gt, ":pos_x", 900),
            (assign, ":pos_x", 100),
            (val_sub, ":pos_y", 100),
          (try_end),
          (overlay_set_position, reg1, pos1),
          (position_set_x, pos1, 80),
          (position_set_y, pos1, 80),
          (overlay_set_size, reg1, pos1),
          (store_sub, ":slot_index", ":cur_charge", custom_banner_charges_begin),
          (troop_set_slot, "trp_temp_array_a", ":slot_index", reg1),
        (try_end),
        (presentation_set_duration, 999999),
        ]),
     (ti_on_presentation_event_state_change,
      [
        (store_trigger_param_1, ":object"),
        (assign, ":end_cond", custom_banner_charges_end),
        (val_sub, ":end_cond", custom_banner_charges_begin),
        (try_for_range, ":trp_slot_index", 0, ":end_cond"),
          (troop_slot_eq, "trp_temp_array_a", ":trp_slot_index", ":object"),
          (troop_set_slot, "trp_player", "$g_presentation_output_slot", ":trp_slot_index"),
          (assign, ":end_cond", 0),
        (try_end),
        (try_begin),
          (gt, "$g_presentation_next_presentation", 0),
          (start_presentation, "$g_presentation_next_presentation"),
        (else_try),
          (presentation_set_duration, 0),
        (try_end),
        ]),
     ]),

  ("banner_background_selection", 0, mesh_load_window,
   [
     (ti_on_presentation_load,
      [
        (set_fixed_point_multiplier, 1000),
        (create_text_overlay, reg1, "str_choose_background", tf_center_justify),
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 600),
        (overlay_set_position, reg1, pos1),
        (assign, ":pos_x", 75),
        (assign, ":pos_y", 450),
        (try_for_range, ":cur_banner", custom_banner_backgrounds_begin, custom_banner_backgrounds_end),
          (store_sub, ":slot_index", ":cur_banner", custom_banner_backgrounds_begin),
          (create_image_button_overlay_with_tableau_material, reg1, -1, "tableau_background_selection", ":slot_index"),
          (position_set_x, pos1, ":pos_x"),
          (position_set_y, pos1, ":pos_y"),
          (val_add, ":pos_x", 125),
          (try_begin),
            (gt, ":pos_x", 900),
            (assign, ":pos_x", 75),
            (val_sub, ":pos_y", 125),
          (try_end),
          (overlay_set_position, reg1, pos1),
          (troop_set_slot, "trp_temp_array_a", ":slot_index", reg1),
        (try_end),
        (presentation_set_duration, 999999),
        ]),
     (ti_on_presentation_event_state_change,
      [
        (store_trigger_param_1, ":object"),
        (assign, ":end_cond", custom_banner_backgrounds_end),
        (val_sub, ":end_cond", custom_banner_backgrounds_begin),
        (try_for_range, ":trp_slot_index", 0, ":end_cond"),
          (troop_slot_eq, "trp_temp_array_a", ":trp_slot_index", ":object"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_bg_type, ":trp_slot_index"),
          (assign, ":end_cond", 0),
        (try_end),
        (try_begin),
          (gt, "$g_presentation_next_presentation", 0),
          (start_presentation, "$g_presentation_next_presentation"),
        (else_try),
          (presentation_set_duration, 0),
        (try_end),
        ]),
     ]),

  ("banner_flag_type_selection", 0, mesh_load_window,
   [
     (ti_on_presentation_load,
      [
        (set_fixed_point_multiplier, 1000),
        (create_text_overlay, reg1, "str_choose_flag_type", tf_center_justify),
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 600),
        (overlay_set_position, reg1, pos1),

        (assign, ":pos_x", 435),
        (assign, ":pos_y", 450),
        (try_for_range, ":cur_flag", custom_banner_flag_types_begin, custom_banner_flag_types_end),
          (store_sub, ":slot_index", ":cur_flag", custom_banner_flag_types_begin),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_flag_type, ":slot_index"),
          (create_image_button_overlay_with_tableau_material, reg1, ":cur_flag", "tableau_custom_banner_default", "trp_player"),
          (position_set_x, pos1, ":pos_x"),
          (position_set_y, pos1, ":pos_y"),
          (val_add, ":pos_x", 130),
          (overlay_set_position, reg1, pos1),
          (position_set_x, pos1, 100),
          (position_set_y, pos1, 100),
          (overlay_set_size, reg1, pos1),
          (troop_set_slot, "trp_temp_array_a", ":slot_index", reg1),
        (try_end),
        (presentation_set_duration, 999999),
        ]),
     (ti_on_presentation_event_state_change,
      [
        (store_trigger_param_1, ":object"),
        (assign, ":end_cond", custom_banner_flag_types_end),
        (val_sub, ":end_cond", custom_banner_flag_types_begin),
        (try_for_range, ":trp_slot_index", 0, ":end_cond"),
          (troop_slot_eq, "trp_temp_array_a", ":trp_slot_index", ":object"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_flag_type, ":trp_slot_index"),
          (assign, ":end_cond", 0),
        (try_end),
        (try_begin),
          (gt, "$g_presentation_next_presentation", 0),
          (start_presentation, "$g_presentation_next_presentation"),
        (else_try),
          (presentation_set_duration, 0),
        (try_end),
        ]),
     ]),

  ("banner_flag_map_type_selection", 0, mesh_load_window,
   [
     (ti_on_presentation_load,
      [
        (set_fixed_point_multiplier, 1000),
        (create_text_overlay, reg1, "str_choose_map_flag_type", tf_center_justify),
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 600),
        (overlay_set_position, reg1, pos1),
        (troop_get_slot, ":bg_type", "trp_player", slot_troop_custom_banner_bg_type),
        (val_add, ":bg_type", custom_banner_backgrounds_begin),
        (assign, ":pos_x", 250),
        (assign, ":pos_y", 150),
        (try_for_range, ":cur_flag", custom_banner_flag_map_types_begin, custom_banner_flag_map_types_end),
          (store_sub, ":slot_index", ":cur_flag", custom_banner_flag_map_types_begin),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_map_flag_type, ":slot_index"),
          (try_begin),
            (eq, ":cur_flag", "mesh_custom_map_banner_01"),
            (create_image_button_overlay_with_tableau_material, reg1, ":cur_flag", "tableau_custom_banner_square", "trp_player"),
          (else_try),
            (eq, ":cur_flag", "mesh_custom_map_banner_02"),
            (create_image_button_overlay_with_tableau_material, reg1, ":cur_flag", "tableau_custom_banner_short", "trp_player"),
          (else_try),
            (create_image_button_overlay_with_tableau_material, reg1, ":cur_flag", "tableau_custom_banner_tall", "trp_player"),
          (try_end),
          (position_set_x, pos1, ":pos_x"),
          (position_set_y, pos1, ":pos_y"),
          (val_add, ":pos_x", 200),
          (overlay_set_position, reg1, pos1),
          (position_set_x, pos1, 100),
          (position_set_y, pos1, 100),
          (overlay_set_size, reg1, pos1),
          (troop_set_slot, "trp_temp_array_a", ":slot_index", reg1),
        (try_end),
        (presentation_set_duration, 999999),
        ]),
     (ti_on_presentation_event_state_change,
      [
        (store_trigger_param_1, ":object"),
        (assign, ":end_cond", custom_banner_flag_map_types_end),
        (val_sub, ":end_cond", custom_banner_flag_map_types_begin),
        (try_for_range, ":trp_slot_index", 0, ":end_cond"),
          (troop_slot_eq, "trp_temp_array_a", ":trp_slot_index", ":object"),
          (troop_set_slot, "trp_player", slot_troop_custom_banner_map_flag_type, ":trp_slot_index"),
          (assign, ":end_cond", 0),
        (try_end),
        (try_begin),
          (gt, "$g_presentation_next_presentation", 0),
          (start_presentation, "$g_presentation_next_presentation"),
        (else_try),
          (presentation_set_duration, 0),
        (try_end),
        ]),
     ]),

  ("color_selection", 0, mesh_load_window,
   [
     (ti_on_presentation_load,
      [
        (set_fixed_point_multiplier, 1000),
        (create_text_overlay, reg1, "str_choose_color", tf_center_justify),
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 600),
        (overlay_set_position, reg1, pos1),

        (assign, ":pos_x", 125),
        (assign, ":pos_y", 450),
        (try_for_range, ":i_color", 0, 42),
          (call_script, "script_get_custom_banner_color_from_index", ":i_color"),
          (assign, ":cur_color", reg0),
          (create_image_button_overlay_with_tableau_material, reg1, "mesh_color_picker", "tableau_color_picker", ":cur_color"),
          (position_set_x, pos1, ":pos_x"),
          (position_set_y, pos1, ":pos_y"),
          (val_add, ":pos_x", 50),
          (try_begin),
            (store_mod, ":mod_i_color", ":i_color", 7),
            (eq, ":mod_i_color", 6),
            (assign, ":pos_x", 125),
            (val_sub, ":pos_y", 50),
          (try_end),
          (overlay_set_position, reg1, pos1),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 500),
          (overlay_set_size, reg1, pos1),
          (store_mul, ":trp_slot_index", ":i_color", 2),
          (store_add, ":trp_slot_color", ":trp_slot_index", 1),
          (troop_set_slot, "trp_temp_array_a", ":trp_slot_index", reg1),
          (troop_set_slot, "trp_temp_array_a", ":trp_slot_color", ":cur_color"),
        (try_end),
        (presentation_set_duration, 999999),
        ]),
     (ti_on_presentation_event_state_change,
      [
        (store_trigger_param_1, ":object"),
        (assign, ":end_cond", 64),
        (try_for_range, ":i_color", 0, ":end_cond"),
          (store_mul, ":trp_slot_index", ":i_color", 2),
          (troop_slot_eq, "trp_temp_array_a", ":trp_slot_index", ":object"),
          (store_add, ":trp_slot_color", ":trp_slot_index", 1),
          (troop_get_slot, ":output_color", "trp_temp_array_a", ":trp_slot_color"),
          (troop_set_slot, "trp_player", "$g_presentation_output_slot", ":output_color"),
          (assign, ":end_cond", 0),
        (try_end),
        (try_begin),
          (gt, "$g_presentation_next_presentation", 0),
          (start_presentation, "$g_presentation_next_presentation"),
        (else_try),
          (presentation_set_duration, 0),
        (try_end),
        ]),
     ]),

  ("marshall_selection", 0, mesh_load_window,
   [
     (ti_on_presentation_load,
      [
        (assign, "$g_presentation_obj_1", -1),
        (assign, "$g_presentation_obj_2", -1),
        (assign, "$g_presentation_obj_3", -1),
        (set_fixed_point_multiplier, 1000),
        (assign, "$g_presentation_next_presentation", -1),

        (store_add, ":total_renown", "$g_presentation_marshall_selection_max_renown_1", "$g_presentation_marshall_selection_max_renown_2"),
        (faction_get_slot, ":king", "$players_kingdom", slot_faction_leader),

        (str_store_troop_name, s1, "$g_presentation_marshall_selection_max_renown_1_troop"),
        (create_text_overlay, reg1, "@Candidate #1: {s1}", tf_center_justify),
        (position_set_x, pos1, 200),
        (position_set_y, pos1, 600),
        (overlay_set_position, reg1, pos1),
        (str_store_troop_name, s1, "$g_presentation_marshall_selection_max_renown_2_troop"),
        (create_text_overlay, reg1, "@Candidate #2: {s1}", tf_center_justify),
        (position_set_x, pos1, 800),
        (position_set_y, pos1, 600),
        (overlay_set_position, reg1, pos1),
        (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_troop_note_mesh", "$g_presentation_marshall_selection_max_renown_1_troop"),
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 500),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 100),
        (position_set_y, pos1, 300),
        (overlay_set_position, reg1, pos1),
        (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_troop_note_mesh", "$g_presentation_marshall_selection_max_renown_2_troop"),
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 500),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 700),
        (position_set_y, pos1, 300),
        (overlay_set_position, reg1, pos1),

        (assign, ":lord_index", "$g_presentation_input"),
        (val_add, "$g_presentation_input", 1),

        (try_begin),
          (lt, ":lord_index", 0),
          (str_store_troop_name, s1, "$g_presentation_marshall_selection_max_renown_1_troop"),
          (str_store_troop_name, s2, "$g_presentation_marshall_selection_max_renown_2_troop"),
          (str_store_troop_name, s3, ":king"),
          (str_store_faction_name, s4, "$players_kingdom"),
          (create_text_overlay, reg1, "@{s3} of {s4} wishes to select a new marshall and invites his vassals for a counsel. {s1} and {s2} are the likely candidates.", tf_center_justify),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 200),
          (overlay_set_position, reg1, pos1),

          (create_button_overlay, "$g_presentation_obj_1", "@Continue...", tf_center_justify),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 100),
          (overlay_set_position, "$g_presentation_obj_1", pos1),
          (assign, "$g_presentation_next_presentation", "prsnt_marshall_selection"),
        (else_try),
          (assign, ":cur_index", ":lord_index"),
          (try_for_range_backwards, ":loop_var", "trp_kingdom_heroes_including_player_begin", kingdom_heroes_end),
            (ge, ":cur_index", 0),
            (assign, ":cur_troop", ":loop_var"),
            (assign, ":continue", 0),
            (try_begin),
              (eq, ":loop_var", "trp_kingdom_heroes_including_player_begin"),
              (assign, ":cur_troop", "trp_player"),
              (try_begin),
                (eq, "$g_player_is_captive", 0),
                (assign, ":continue", 1),
              (try_end),
            (else_try),
              (store_troop_faction, ":cur_troop_faction", ":cur_troop"),
              (eq, "$players_kingdom", ":cur_troop_faction"),
              #(troop_slot_eq, ":cur_troop", slot_troop_is_prisoner, 0),
              (neg|troop_slot_ge, ":cur_troop", slot_troop_prisoner_of_party, 0),
              (troop_slot_ge, ":cur_troop", slot_troop_leaded_party, 1),
              (troop_slot_eq, ":cur_troop", slot_troop_occupation, slto_kingdom_hero),
              (neg|faction_slot_eq, ":cur_troop_faction", slot_faction_leader, ":cur_troop"),
              (assign, ":continue", 1),
            (try_end),
            (eq, ":continue", 1),

            (neq, "$g_presentation_marshall_selection_max_renown_1_troop", ":cur_troop"),
            (neq, "$g_presentation_marshall_selection_max_renown_2_troop", ":cur_troop"),

            (val_sub, ":cur_index", 1),
            (lt, ":cur_index", 0),

            (assign, "$g_presentation_next_presentation", "prsnt_marshall_selection"),

            (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_troop_note_mesh", ":cur_troop"),
            (position_set_x, pos1, 300),
            (position_set_y, pos1, 300),
            (overlay_set_size, reg1, pos1),
            (position_set_x, pos1, 440),
            (position_set_y, pos1, 400),
            (overlay_set_position, reg1, pos1),

            (try_begin),
              (eq, ":cur_troop", "trp_player"),
              (create_text_overlay, reg1, "@Who do you wish to support?", tf_center_justify),
              (position_set_x, pos1, 500),
              (position_set_y, pos1, 200),
              (overlay_set_position, reg1, pos1),
              (str_store_troop_name, s1, "$g_presentation_marshall_selection_max_renown_1_troop"),
              (create_button_overlay, "$g_presentation_obj_2", "@{s1}", tf_center_justify),
              (position_set_x, pos1, 300),
              (position_set_y, pos1, 100),
              (overlay_set_position, "$g_presentation_obj_2", pos1),
              (str_store_troop_name, s1, "$g_presentation_marshall_selection_max_renown_2_troop"),
              (create_button_overlay, "$g_presentation_obj_3", "@{s1}", tf_center_justify),
              (position_set_x, pos1, 700),
              (position_set_y, pos1, 100),
              (overlay_set_position, "$g_presentation_obj_3", pos1),
            (else_try),
              (str_store_troop_name, s1, ":cur_troop"),
              (store_random_in_range, ":random_renown", 0, ":total_renown"),
              (val_sub, ":random_renown", "$g_presentation_marshall_selection_max_renown_1"),
              (try_begin),
                (lt, ":random_renown", 0),
                (val_add, "$g_presentation_marshall_selection_1_vote", 1),
                (str_store_troop_name, s2, "$g_presentation_marshall_selection_max_renown_1_troop"),
              (else_try),
                (val_add, "$g_presentation_marshall_selection_2_vote", 1),
                (str_store_troop_name, s2, "$g_presentation_marshall_selection_max_renown_2_troop"),
              (try_end),
              (create_text_overlay, reg1, "@{s1} gives his support to {s2}.", tf_center_justify),
              (position_set_x, pos1, 500),
              (position_set_y, pos1, 200),
              (overlay_set_position, reg1, pos1),

              (create_button_overlay, "$g_presentation_obj_1", "@Continue...", tf_center_justify),
              (position_set_x, pos1, 500),
              (position_set_y, pos1, 100),
              (overlay_set_position, "$g_presentation_obj_1", pos1),
            (try_end),
          (try_end),
        (try_end),

        (assign, reg0, "$g_presentation_marshall_selection_1_vote"),
        (create_text_overlay, reg1, "@Number of Supporters: {reg0}", tf_center_justify),
        (position_set_x, pos1, 200),
        (position_set_y, pos1, 550),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, "$g_presentation_marshall_selection_2_vote"),
        (create_text_overlay, reg1, "@Number of Supporters: {reg0}", tf_center_justify),
        (position_set_x, pos1, 800),
        (position_set_y, pos1, 550),
        (overlay_set_position, reg1, pos1),

        (try_begin),
          (lt, "$g_presentation_next_presentation", 0),
          (try_begin),
            (gt, "$g_presentation_marshall_selection_2_vote", "$g_presentation_marshall_selection_1_vote"),
            (assign, ":local_temp", "$g_presentation_marshall_selection_max_renown_1_troop"),
            (assign, "$g_presentation_marshall_selection_max_renown_1_troop", "$g_presentation_marshall_selection_max_renown_2_troop"),
            (assign, "$g_presentation_marshall_selection_max_renown_2_troop", ":local_temp"),
          (try_end),
          #Winner is set to g_presentation_marshall_selection_max_renown_1_troop
          (str_store_troop_name, s1, "$g_presentation_marshall_selection_max_renown_1_troop"),
          (str_store_troop_name, s2, ":king"),
          (str_store_faction_name, s3, "$players_kingdom"),
          (create_text_overlay, reg1, "@{s2} has heard his vassals' counsel. He selects {s1} as the marshall of {s3}.", tf_center_justify),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 200),
          (overlay_set_position, reg1, pos1),
          (create_button_overlay, "$g_presentation_obj_1", "@Continue...", tf_center_justify),
          (position_set_x, pos1, 500),
          (position_set_y, pos1, 100),
          (overlay_set_position, "$g_presentation_obj_1", pos1),
        (try_end),

        (presentation_set_duration, 999999),
        ]),
     (ti_on_presentation_event_state_change,
      [
        (store_trigger_param_1, ":object"),
        (try_begin),
          (eq, ":object", "$g_presentation_obj_2"),
          (val_add, "$g_presentation_marshall_selection_1_vote", 1),
          (call_script, "script_change_player_relation_with_troop", "$g_presentation_marshall_selection_max_renown_1_troop", 2),
          (call_script, "script_change_player_relation_with_troop", "$g_presentation_marshall_selection_max_renown_2_troop", -2),
        (else_try),
          (eq, ":object", "$g_presentation_obj_3"),
          (val_add, "$g_presentation_marshall_selection_2_vote", 1),
          (call_script, "script_change_player_relation_with_troop", "$g_presentation_marshall_selection_max_renown_1_troop", -2),
          (call_script, "script_change_player_relation_with_troop", "$g_presentation_marshall_selection_max_renown_2_troop", 2),
        (try_end),
        (try_begin),
          (this_or_next|eq, ":object", "$g_presentation_obj_1"),
          (this_or_next|eq, ":object", "$g_presentation_obj_2"),
          (eq, ":object", "$g_presentation_obj_3"),
          (try_begin),
            (ge, "$g_presentation_next_presentation", 0),
            (start_presentation, "$g_presentation_next_presentation"),
          (else_try),
            (try_begin),
              (neg|faction_slot_eq, "$players_kingdom", slot_faction_marshall, "$g_presentation_marshall_selection_max_renown_1_troop"),
              (call_script, "script_check_and_finish_active_army_quests_for_faction", "$players_kingdom"),
            (try_end),
            (faction_set_slot, "$players_kingdom", slot_faction_marshall, "$g_presentation_marshall_selection_max_renown_1_troop"),
            (try_begin),
              (eq, "$g_presentation_marshall_selection_max_renown_1_troop", "trp_player"),
              (call_script, "script_change_player_relation_with_troop", "$g_presentation_marshall_selection_max_renown_2_troop", -3),
            (try_end),
            (assign, "$g_recalculate_ais", 1),
            (assign, "$g_presentation_marshall_selection_ended", 1),
            (presentation_set_duration, 0),
          (try_end),
        (try_end),
        ]),
     ]),
  
  ("battle",0,0,[
      (ti_on_presentation_load,
       [(set_fixed_point_multiplier, 1000),
        (assign, "$g_formation_infantry_selected", 0),
        (assign, "$g_formation_archers_selected", 0),
        (assign, "$g_formation_cavalry_selected", 0),
        (assign, "$g_presentation_battle_active", 1),

        (str_clear, s7),

        (try_for_agents, ":agent_no"),
          (agent_set_slot, ":agent_no", slot_agent_map_overlay_id, 0),
        (try_end),

        (position_set_x, pos1, 0),
        (create_image_button_overlay, "$g_presentation_obj_1", "mesh_white_plane", "mesh_white_plane"),
        (position_set_y, pos1, 653),
        (overlay_set_position, "$g_presentation_obj_1", pos1),
        (create_image_button_overlay, "$g_presentation_obj_2", "mesh_white_plane", "mesh_white_plane"),
        (position_set_y, pos1, 613),
        (overlay_set_position, "$g_presentation_obj_2", pos1),
        (create_image_button_overlay, "$g_presentation_obj_3", "mesh_white_plane", "mesh_white_plane"),
        (position_set_y, pos1, 573),
        (overlay_set_position, "$g_presentation_obj_3", pos1),
        (position_set_x, pos1, 32500),
        (position_set_y, pos1, 2000),
        (overlay_set_size, "$g_presentation_obj_1", pos1),
        (overlay_set_size, "$g_presentation_obj_2", pos1),
        (overlay_set_size, "$g_presentation_obj_3", pos1),
        (overlay_set_alpha, "$g_presentation_obj_1", 0),
        (overlay_set_alpha, "$g_presentation_obj_2", 0),
        (overlay_set_alpha, "$g_presentation_obj_3", 0),
        (overlay_set_color, "$g_presentation_obj_1", 0xFFFF00),
        (overlay_set_color, "$g_presentation_obj_2", 0xFFFF00),
        (overlay_set_color, "$g_presentation_obj_3", 0xFFFF00),
        
        (position_set_x, pos1, 20),
        (create_check_box_overlay, "$g_presentation_obj_4", "mesh_checkbox_off", "mesh_checkbox_on"),
        (position_set_y, pos1, 663),
        (overlay_set_position, "$g_presentation_obj_4", pos1),
        (create_check_box_overlay, "$g_presentation_obj_5", "mesh_checkbox_off", "mesh_checkbox_on"),
        (position_set_y, pos1, 623),
        (overlay_set_position, "$g_presentation_obj_5", pos1),
        (create_check_box_overlay, "$g_presentation_obj_6", "mesh_checkbox_off", "mesh_checkbox_on"),
        (position_set_y, pos1, 583),
        (overlay_set_position, "$g_presentation_obj_6", pos1),

        (get_player_agent_no, ":player_agent"),
        (agent_get_team, ":player_team", ":player_agent"),
        
        (position_set_x, pos1, 50),
        (create_text_overlay, "$g_presentation_obj_7", s7, 0),
        (position_set_y, pos1, 660),
        (overlay_set_position, "$g_presentation_obj_7", pos1),
        (create_text_overlay, "$g_presentation_obj_8", s7, 0),
        (position_set_y, pos1, 620),
        (overlay_set_position, "$g_presentation_obj_8", pos1),
        (create_text_overlay, "$g_presentation_obj_9", s7, 0),
        (position_set_y, pos1, 580),
        (overlay_set_position, "$g_presentation_obj_9", pos1),

        (position_set_y, pos1, 700),
        (create_text_overlay, reg1, "@Action", tf_center_justify),
        (position_set_x, pos1, 250),
        (overlay_set_position, reg1, pos1),
        (create_text_overlay, reg1, "@Mount", tf_center_justify),
        (position_set_x, pos1, 400),
        (overlay_set_position, reg1, pos1),
        (create_text_overlay, reg1, "@Attack Type", tf_center_justify),
        (position_set_x, pos1, 550),
        (overlay_set_position, reg1, pos1),

        (create_text_overlay, "$g_presentation_infantry_movement", s7, tf_center_justify),
        (create_text_overlay, "$g_presentation_infantry_riding", s7, tf_center_justify),
        (create_text_overlay, "$g_presentation_infantry_weapon_usage", s7, tf_center_justify),
        (create_text_overlay, "$g_presentation_archers_movement", s7, tf_center_justify),
        (create_text_overlay, "$g_presentation_archers_riding", s7, tf_center_justify),
        (create_text_overlay, "$g_presentation_archers_weapon_usage", s7, tf_center_justify),
        (create_text_overlay, "$g_presentation_cavalry_movement", s7, tf_center_justify),
        (create_text_overlay, "$g_presentation_cavalry_riding", s7, tf_center_justify),
        (create_text_overlay, "$g_presentation_cavalry_weapon_usage", s7, tf_center_justify),

        (call_script, "script_update_order_panel", ":player_team"),

        (create_button_overlay, "$g_presentation_obj_10", "@Hold This Position", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_11", "@Follow Me", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_12", "@Charge", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_13", "@Advance", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_14", "@Fall Back", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_15", "@Stand Closer", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_16", "@Spread Out", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_17", "@Stand Ground", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_18", "@Mount", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_19", "@Dismount", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_20", "@Use Any Weapon", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_21", "@Use Blunt Weapons", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_28", "@Hold Your Fire", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_29", "@Fire At Will", tf_center_justify),

        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, "$g_presentation_obj_10", pos1),
        (overlay_set_size, "$g_presentation_obj_11", pos1),
        (overlay_set_size, "$g_presentation_obj_12", pos1),
        (overlay_set_size, "$g_presentation_obj_13", pos1),
        (overlay_set_size, "$g_presentation_obj_14", pos1),
        (overlay_set_size, "$g_presentation_obj_15", pos1),
        (overlay_set_size, "$g_presentation_obj_16", pos1),
        (overlay_set_size, "$g_presentation_obj_17", pos1),
        (overlay_set_size, "$g_presentation_obj_18", pos1),
        (overlay_set_size, "$g_presentation_obj_19", pos1),
        (overlay_set_size, "$g_presentation_obj_20", pos1),
        (overlay_set_size, "$g_presentation_obj_21", pos1),
        (overlay_set_size, "$g_presentation_obj_28", pos1),
        (overlay_set_size, "$g_presentation_obj_29", pos1),
        
        (position_set_x, pos1, 250),
        (position_set_y, pos1, 540),
        (overlay_set_position, "$g_presentation_obj_10", pos1),
        (position_set_y, pos1, 520),
        (overlay_set_position, "$g_presentation_obj_11", pos1),
        (position_set_y, pos1, 500),
        (overlay_set_position, "$g_presentation_obj_12", pos1),
        (position_set_y, pos1, 480),
        (overlay_set_position, "$g_presentation_obj_13", pos1),
        (position_set_y, pos1, 460),
        (overlay_set_position, "$g_presentation_obj_14", pos1),
        (position_set_y, pos1, 440),
        (overlay_set_position, "$g_presentation_obj_15", pos1),
        (position_set_y, pos1, 420),
        (overlay_set_position, "$g_presentation_obj_16", pos1),
        (position_set_y, pos1, 400),
        (overlay_set_position, "$g_presentation_obj_17", pos1),

        (position_set_x, pos1, 400),
        (position_set_y, pos1, 540),
        (overlay_set_position, "$g_presentation_obj_18", pos1),
        (position_set_y, pos1, 520),
        (overlay_set_position, "$g_presentation_obj_19", pos1),
        
        (position_set_x, pos1, 550),
        (position_set_y, pos1, 540),
        (overlay_set_position, "$g_presentation_obj_20", pos1),
        (position_set_y, pos1, 520),
        (overlay_set_position, "$g_presentation_obj_21", pos1),
        (position_set_y, pos1, 500),
        (overlay_set_position, "$g_presentation_obj_28", pos1),
        (position_set_y, pos1, 480),
        (overlay_set_position, "$g_presentation_obj_29", pos1),

        (create_text_overlay, "$g_presentation_obj_22", "@Us", 0),
        (create_text_overlay, "$g_presentation_obj_24", "@Enemies", 0),
        (create_text_overlay, "$g_presentation_obj_25", "@Ready", tf_center_justify),
        (create_text_overlay, "$g_presentation_obj_26", "@Wounded", tf_center_justify),
        (create_text_overlay, "$g_presentation_obj_27", "@Dead", tf_center_justify),

        (create_text_overlay, "$g_battle_us_ready", s7, tf_center_justify),
        (create_text_overlay, "$g_battle_us_wounded", s7, tf_center_justify),
        (create_text_overlay, "$g_battle_us_dead", s7, tf_center_justify),
        (create_text_overlay, "$g_battle_enemies_ready", s7, tf_center_justify),
        (create_text_overlay, "$g_battle_enemies_wounded", s7, tf_center_justify),
        (create_text_overlay, "$g_battle_enemies_dead", s7, tf_center_justify),

        (create_text_overlay, "$g_presentation_obj_23", "@Allies", 0),
        (create_text_overlay, "$g_battle_allies_ready", s7, tf_center_justify),
        (create_text_overlay, "$g_battle_allies_wounded", s7, tf_center_justify),
        (create_text_overlay, "$g_battle_allies_dead", s7, tf_center_justify),

        (assign, ":stat_position_x", 100),
        (assign, ":stat_position_y", 100),

        (val_add, ":stat_position_x", 150),
        (val_add, ":stat_position_y", 120),
        (position_set_x, pos1, ":stat_position_x"),
        (position_set_y, pos1, ":stat_position_y"),
        (overlay_set_position, "$g_presentation_obj_25", pos1),
        (val_add, ":stat_position_x", 150),
        (position_set_x, pos1, ":stat_position_x"),
        (overlay_set_position, "$g_presentation_obj_26", pos1),
        (val_add, ":stat_position_x", 150),
        (position_set_x, pos1, ":stat_position_x"),
        (overlay_set_position, "$g_presentation_obj_27", pos1),

        (val_add, ":stat_position_x", -450),
        (val_add, ":stat_position_y", -40),
        (position_set_x, pos1, ":stat_position_x"),
        (position_set_y, pos1, ":stat_position_y"),
        (overlay_set_position, "$g_presentation_obj_22", pos1),
        (val_add, ":stat_position_y", -40),
        (position_set_y, pos1, ":stat_position_y"),
        (overlay_set_position, "$g_presentation_obj_23", pos1),
        (val_add, ":stat_position_y", -40),
        (position_set_y, pos1, ":stat_position_y"),
        (overlay_set_position, "$g_presentation_obj_24", pos1),

        (get_scene_boundaries, pos2, pos3),
        (position_transform_position_to_local, pos4, pos2, pos3),
        (set_fixed_point_multiplier, 1000),
        (position_get_x, ":map_width", pos4),
        (position_get_y, ":map_height", pos4),
        (set_fixed_point_multiplier, 1000),
        (store_div, ":map_ratio", ":map_height", 100),
        (store_div, ":map_ratio", ":map_width", ":map_ratio"),
        (try_begin),
          (gt, ":map_ratio", 100),
          (assign, "$g_battle_map_width", 300),
          (store_div, "$g_battle_map_scale", ":map_width", "$g_battle_map_width"),
          (store_div, "$g_battle_map_height", ":map_height", "$g_battle_map_scale"),
        (else_try),
          (assign, "$g_battle_map_height", 300),
          (store_div, "$g_battle_map_scale", ":map_height", "$g_battle_map_height"),
          (store_div, "$g_battle_map_width", ":map_width", "$g_battle_map_scale"),
        (try_end),
        
        (create_image_button_overlay, "$g_battle_map_plane", "mesh_white_plane", "mesh_white_plane"),
        (overlay_set_color, "$g_battle_map_plane", 0),
        (store_add, ":map_bordered_width", "$g_battle_map_width", 20),
        (store_add, ":map_bordered_height", "$g_battle_map_height", 20),
        (store_mul, ":map_scale_x", ":map_bordered_width", 50),
        (store_mul, ":map_scale_y", ":map_bordered_height", 50),
        (position_set_x, pos1, ":map_scale_x"),
        (position_set_y, pos1, ":map_scale_y"),
        (overlay_set_size, "$g_battle_map_plane", pos1),
        (store_sub, ":map_pos_x", 990, ":map_bordered_width"),
        (store_sub, ":map_pos_y", 740, ":map_bordered_height"),
        (position_set_x, pos1, ":map_pos_x"),
        (position_set_y, pos1, ":map_pos_y"),
        (overlay_set_position, "$g_battle_map_plane", pos1),
        (overlay_set_alpha, "$g_battle_map_plane", 0x44),

        (create_mesh_overlay, "$g_battle_map_infantry_order_flag", "mesh_flag_infantry"),
        (create_mesh_overlay, "$g_battle_map_archers_order_flag", "mesh_flag_archers"),
        (create_mesh_overlay, "$g_battle_map_cavalry_order_flag", "mesh_flag_cavalry"),
        (overlay_set_alpha, "$g_battle_map_infantry_order_flag", 0),
        (overlay_set_alpha, "$g_battle_map_archers_order_flag", 0),
        (overlay_set_alpha, "$g_battle_map_cavalry_order_flag", 0),

        (call_script, "script_update_order_panel_checked_classes"),
        (call_script, "script_update_order_panel_statistics_and_map"),

        (presentation_set_duration, 999999),
        ]),
      (ti_on_presentation_event_state_change,
       [(store_trigger_param_1, ":object"),
        (store_trigger_param_2, ":value"),
        (get_player_agent_no, ":player_agent"),
        (agent_get_team, ":player_team", ":player_agent"),

        (assign, ":update_listeners", 0),
        (try_begin),
          (eq, ":object", "$g_presentation_obj_4"),
          (assign, ":update_listeners", 1),
          (assign, "$g_formation_infantry_selected", ":value"),
          (try_begin),
            (eq, ":value", 1),
            (overlay_animate_to_alpha, "$g_presentation_obj_1", 250, 0x44),
          (else_try),
            (overlay_animate_to_alpha, "$g_presentation_obj_1", 250, 0),
          (try_end),
        (else_try),
          (eq, ":object", "$g_presentation_obj_5"),
          (assign, ":update_listeners", 1),
          (assign, "$g_formation_archers_selected", ":value"),
          (try_begin),
            (eq, ":value", 1),
            (overlay_animate_to_alpha, "$g_presentation_obj_2", 250, 0x44),
          (else_try),
            (overlay_animate_to_alpha, "$g_presentation_obj_2", 250, 0),
          (try_end),
        (else_try),
          (eq, ":object", "$g_presentation_obj_6"),
          (assign, ":update_listeners", 1),
          (assign, "$g_formation_cavalry_selected", ":value"),
          (try_begin),
            (eq, ":value", 1),
            (overlay_animate_to_alpha, "$g_presentation_obj_3", 250, 0x44),
          (else_try),
            (overlay_animate_to_alpha, "$g_presentation_obj_3", 250, 0),
          (try_end),
        (else_try),
          (eq, ":object", "$g_presentation_obj_1"),
          (assign, ":update_listeners", 1),
          (assign, "$g_formation_infantry_selected", 1),
          (assign, "$g_formation_archers_selected", 0),
          (assign, "$g_formation_cavalry_selected", 0),
          (overlay_animate_to_alpha, "$g_presentation_obj_1", 250, 0x44),
          (overlay_animate_to_alpha, "$g_presentation_obj_2", 250, 0),
          (overlay_animate_to_alpha, "$g_presentation_obj_3", 250, 0),
          (overlay_set_val, "$g_presentation_obj_4", 1),
          (overlay_set_val, "$g_presentation_obj_5", 0),
          (overlay_set_val, "$g_presentation_obj_6", 0),
        (else_try),
          (eq, ":object", "$g_presentation_obj_2"),
          (assign, ":update_listeners", 1),
          (assign, "$g_formation_infantry_selected", 0),
          (assign, "$g_formation_archers_selected", 1),
          (assign, "$g_formation_cavalry_selected", 0),
          (overlay_animate_to_alpha, "$g_presentation_obj_1", 250, 0),
          (overlay_animate_to_alpha, "$g_presentation_obj_2", 250, 0x44),
          (overlay_animate_to_alpha, "$g_presentation_obj_3", 250, 0),
          (overlay_set_val, "$g_presentation_obj_4", 0),
          (overlay_set_val, "$g_presentation_obj_5", 1),
          (overlay_set_val, "$g_presentation_obj_6", 0),
        (else_try),
          (eq, ":object", "$g_presentation_obj_3"),
          (assign, ":update_listeners", 1),
          (assign, "$g_formation_infantry_selected", 0),
          (assign, "$g_formation_archers_selected", 0),
          (assign, "$g_formation_cavalry_selected", 1),
          (overlay_animate_to_alpha, "$g_presentation_obj_1", 250, 0),
          (overlay_animate_to_alpha, "$g_presentation_obj_2", 250, 0),
          (overlay_animate_to_alpha, "$g_presentation_obj_3", 250, 0x44),
          (overlay_set_val, "$g_presentation_obj_4", 0),
          (overlay_set_val, "$g_presentation_obj_5", 0),
          (overlay_set_val, "$g_presentation_obj_6", 1),
        (else_try),
          (eq, ":object", "$g_presentation_obj_10"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_hold),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_11"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_follow),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_12"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_charge),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_13"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_advance),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_14"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_fall_back),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_15"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_stand_closer),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_16"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_spread_out),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_17"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_stand_ground),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_18"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_mount),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_19"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_dismount),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_20"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_use_any_weapon),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_21"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_use_blunt_weapons),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_28"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_hold_fire),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_29"),
          (call_script, "script_team_give_order_from_order_panel", ":player_agent", mordr_fire_at_will),
          (call_script, "script_update_order_panel", ":player_team"),
        (else_try),
          (eq, ":object", "$g_battle_map_plane"),
          (get_scene_boundaries, pos2, pos3),
          (mouse_get_position, pos1),
          (set_fixed_point_multiplier, 1000),
          (position_get_x, ":mouse_x", pos1),
          (position_get_y, ":mouse_y", pos1),
          (store_sub, ":map_x", 980, "$g_battle_map_width"),
          (store_sub, ":map_y", 730, "$g_battle_map_height"),
          (val_sub, ":mouse_x", ":map_x"),
          (val_sub, ":mouse_y", ":map_y"),
          (val_clamp, ":mouse_x", 0, "$g_battle_map_width"),
          (val_clamp, ":mouse_y", 0, "$g_battle_map_height"),
          (val_mul, ":mouse_x", "$g_battle_map_scale"),
          (val_mul, ":mouse_y", "$g_battle_map_scale"),
          (set_fixed_point_multiplier, 1000),
          (position_set_x, pos1, ":mouse_x"),
          (position_set_y, pos1, ":mouse_y"),
          (position_transform_position_to_parent, pos3, pos2, pos1),
          (try_begin),
            (eq, "$g_formation_infantry_selected", 1),
            (team_give_order, ":player_team", grc_infantry, mordr_hold),
            (team_set_order_position, ":player_team", grc_infantry, pos3),
          (try_end),
          (try_begin),
            (eq, "$g_formation_archers_selected", 1),
            (team_give_order, ":player_team", grc_archers, mordr_hold),
            (team_set_order_position, ":player_team", grc_archers, pos3),
          (try_end),
          (try_begin),
            (eq, "$g_formation_cavalry_selected", 1),
            (team_give_order, ":player_team", grc_cavalry, mordr_hold),
            (team_set_order_position, ":player_team", grc_cavalry, pos3),
          (try_end),
          (call_script, "script_update_order_flags_on_map"),
          (call_script, "script_update_order_panel", ":player_team"),
        (try_end),
        (try_begin),
          (eq, ":update_listeners", 1),
          (try_begin),
            (eq, "$g_formation_infantry_selected", 1),
            (eq, "$g_formation_archers_selected", 1),
            (eq, "$g_formation_cavalry_selected", 1),
            (team_set_order_listener, ":player_team", grc_everyone),
          (else_try),
            (eq, "$g_formation_infantry_selected", 1),
            (team_set_order_listener, ":player_team", grc_infantry),
          (else_try),
            (eq, "$g_formation_archers_selected", 1),
            (team_set_order_listener, ":player_team", grc_archers),
          (else_try),
            (eq, "$g_formation_cavalry_selected", 1),
            (team_set_order_listener, ":player_team", grc_cavalry),
          (try_end),
        (try_end),
        ]),
      (ti_on_presentation_mouse_enter_leave,
       [(store_trigger_param_1, ":object"),
        (store_trigger_param_2, ":enter_leave"),
        (try_begin),
          (eq, ":object", "$g_presentation_obj_1"),
          (try_begin),
            (eq, ":enter_leave", 0),
            (overlay_animate_to_color, "$g_presentation_infantry_movement", 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_infantry_riding", 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_infantry_weapon_usage", 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_obj_7", 250, 0xFFFFFF),
          (else_try),
            (overlay_animate_to_color, "$g_presentation_infantry_movement", 250, 0),
            (overlay_animate_to_color, "$g_presentation_infantry_riding", 250, 0),
            (overlay_animate_to_color, "$g_presentation_infantry_weapon_usage", 250, 0),
            (overlay_animate_to_color, "$g_presentation_obj_7", 250, 0),
          (try_end),
        (else_try),
          (eq, ":object", "$g_presentation_obj_2"),
          (try_begin),
            (eq, ":enter_leave", 0),
            (overlay_animate_to_color, "$g_presentation_archers_movement", 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_archers_riding", 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_archers_weapon_usage", 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_obj_8", 250, 0xFFFFFF),
          (else_try),
            (overlay_animate_to_color, "$g_presentation_archers_movement", 250, 0),
            (overlay_animate_to_color, "$g_presentation_archers_riding", 250, 0),
            (overlay_animate_to_color, "$g_presentation_archers_weapon_usage", 250, 0),
            (overlay_animate_to_color, "$g_presentation_obj_8", 250, 0),
          (try_end),
        (else_try),
          (eq, ":object", "$g_presentation_obj_3"),
          (try_begin),
            (eq, ":enter_leave", 0),
            (overlay_animate_to_color, "$g_presentation_cavalry_movement", 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_cavalry_riding", 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_cavalry_weapon_usage", 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_obj_9", 250, 0xFFFFFF),
          (else_try),
            (overlay_animate_to_color, "$g_presentation_cavalry_movement", 250, 0),
            (overlay_animate_to_color, "$g_presentation_cavalry_riding", 250, 0),
            (overlay_animate_to_color, "$g_presentation_cavalry_weapon_usage", 250, 0),
            (overlay_animate_to_color, "$g_presentation_obj_9", 250, 0),
          (try_end),
        (try_end),
        ]),
      (ti_on_presentation_run,
       [(store_trigger_param_1, ":cur_time"),
        (try_begin),
          (this_or_next|game_key_clicked, gk_infantry_hear),
          (this_or_next|game_key_clicked, gk_archers_hear),
          (this_or_next|game_key_clicked, gk_cavalry_hear),
          (this_or_next|game_key_clicked, gk_everyone_hear),
          (game_key_clicked, gk_reverse_order_group),
          (call_script, "script_update_order_panel_checked_classes"),
        (try_end),
        (try_begin),
          (this_or_next|game_key_clicked, gk_order_halt),
          (this_or_next|game_key_clicked, gk_order_follow),
          (this_or_next|game_key_clicked, gk_order_charge),
          (this_or_next|game_key_clicked, gk_order_dismount),
          (this_or_next|game_key_clicked, gk_order_advance),
          (this_or_next|game_key_clicked, gk_order_fall_back),
          (this_or_next|game_key_clicked, gk_order_stand_closer),
          (this_or_next|game_key_clicked, gk_order_spread_out),
          (this_or_next|game_key_clicked, gk_order_hold_fire_toggle),
          (game_key_clicked, gk_order_blunt_weapons_toggle),
          (get_player_agent_no, ":player_agent"),
          (agent_get_team, ":player_team", ":player_agent"),
          (call_script, "script_update_order_panel", ":player_team"),
        (try_end),
        (try_begin),
          (gt, ":cur_time", 200),
          (game_key_clicked, gk_view_orders),
          (assign, "$g_presentation_battle_active", 0),
          (try_for_agents, ":cur_agent"),
            (agent_set_slot, ":cur_agent", slot_agent_map_overlay_id, 0),
          (try_end),
          (presentation_set_duration, 0),
        (try_end),
        ]),
      ]),


    ("sliders",0,0,[
      (ti_on_presentation_load,
       [(set_fixed_point_multiplier, 1000),
        (create_slider_overlay, "$g_presentation_obj_1", 0, 100),
        (create_slider_overlay, "$g_presentation_obj_2", 0, 100),
        (create_slider_overlay, "$g_presentation_obj_3", 0, 100),
        (create_slider_overlay, "$g_presentation_obj_4", 0, 100),
        (assign, reg1, 25),
        (str_store_string, s1, "str_reg1"),
        (create_text_overlay, "$g_presentation_obj_5", s1),
        (create_text_overlay, "$g_presentation_obj_6", s1),
        (create_text_overlay, "$g_presentation_obj_7", s1),
        (create_text_overlay, "$g_presentation_obj_8", s1),
        (assign, "$g_presentation_obj_1_val", 25),
        (overlay_set_val, "$g_presentation_obj_1", 25),
        (assign, "$g_presentation_obj_2_val", 25),
        (overlay_set_val, "$g_presentation_obj_2", 25),
        (assign, "$g_presentation_obj_3_val", 25),
        (overlay_set_val, "$g_presentation_obj_3", 25),
        (assign, "$g_presentation_obj_4_val", 25),
        (overlay_set_val, "$g_presentation_obj_4", 25),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 200),
        (overlay_set_position, "$g_presentation_obj_1", pos1),
        (position_set_y, pos1, 300),
        (overlay_set_position, "$g_presentation_obj_2", pos1),
        (position_set_y, pos1, 400),
        (overlay_set_position, "$g_presentation_obj_3", pos1),
        (position_set_y, pos1, 500),
        (overlay_set_position, "$g_presentation_obj_4", pos1),
        (position_set_x, pos1, 800),
        (position_set_y, pos1, 200),
        (overlay_set_position, "$g_presentation_obj_5", pos1),
        (position_set_y, pos1, 300),
        (overlay_set_position, "$g_presentation_obj_6", pos1),
        (position_set_y, pos1, 400),
        (overlay_set_position, "$g_presentation_obj_7", pos1),
        (position_set_y, pos1, 500),
        (overlay_set_position, "$g_presentation_obj_8", pos1),
        (position_set_x, pos1, 500),
        (overlay_set_size, "$g_presentation_obj_3", pos1),
        ]),
      (ti_on_presentation_run,
       [
        ]),
      (ti_on_presentation_event_state_change,
       [(store_trigger_param_1, ":object"),
        (store_trigger_param_2, ":value"),
        (assign, ":val_changed", 0),
        (try_begin),
          (eq, ":object", "$g_presentation_obj_1"),
          (try_begin),
            (neq, "$g_presentation_obj_1_val", ":value"),
            (assign, "$g_presentation_obj_1_val", ":value"),
            (assign, ":val_changed", 1),
          (try_end),
        (else_try),
          (eq, ":object", "$g_presentation_obj_2"),
          (try_begin),
            (neq, "$g_presentation_obj_2_val", ":value"),
            (assign, "$g_presentation_obj_2_val", ":value"),
            (assign, ":val_changed", 1),
          (try_end),
        (else_try),
          (eq, ":object", "$g_presentation_obj_3"),
          (try_begin),
            (neq, "$g_presentation_obj_3_val", ":value"),
            (assign, "$g_presentation_obj_3_val", ":value"),
            (assign, ":val_changed", 1),
          (try_end),
        (else_try),
          (eq, ":object", "$g_presentation_obj_4"),
          (try_begin),
            (neq, "$g_presentation_obj_4_val", ":value"),
            (assign, "$g_presentation_obj_4_val", ":value"),
            (assign, ":val_changed", 1),
          (try_end),
        (try_end),
        (try_begin),
          (eq, ":val_changed", 1),
          (assign, ":sum_values", 0),
          (val_add, ":sum_values", "$g_presentation_obj_1_val"),
          (val_add, ":sum_values", "$g_presentation_obj_2_val"),
          (val_add, ":sum_values", "$g_presentation_obj_3_val"),
          (val_add, ":sum_values", "$g_presentation_obj_4_val"),
          (store_sub, ":difference", 100, ":sum_values"),
          (assign, ":obj_1_new_val", "$g_presentation_obj_1_val"),
          (assign, ":obj_2_new_val", "$g_presentation_obj_2_val"),
          (assign, ":obj_3_new_val", "$g_presentation_obj_3_val"),
          (assign, ":obj_4_new_val", "$g_presentation_obj_4_val"),
          (assign, ":adder", 1),
          (assign, ":between_1", 0),
          (assign, ":between_2", 100),
          (try_begin),
            (lt, ":difference", 0),
            (val_mul, ":difference", -1),
            (assign, ":adder", -1),
            (assign, ":between_1", 1),
            (assign, ":between_2", 101),
          (try_end),
          (store_sub, ":sum_non_changed_values", ":sum_values", ":value"),
          (val_add, ":sum_non_changed_values", 30),
          (try_for_range, ":unused", 0, ":difference"),
            (store_random_in_range, ":random_no", 0, ":sum_non_changed_values"),
            (try_begin),
              (neq, ":object", "$g_presentation_obj_1"),
              (val_sub, ":random_no", "$g_presentation_obj_1_val"),
              (val_sub, ":random_no", 10),
              (lt, ":random_no", 0),
              (is_between, ":obj_1_new_val", ":between_1", ":between_2"),
              (val_add, ":obj_1_new_val", ":adder"),
            (else_try),
              (neq, ":object", "$g_presentation_obj_2"),
              (val_sub, ":random_no", "$g_presentation_obj_2_val"),
              (val_sub, ":random_no", 10),
              (lt, ":random_no", 0),
              (is_between, ":obj_2_new_val", ":between_1", ":between_2"),
              (val_add, ":obj_2_new_val", ":adder"),
            (else_try),
              (neq, ":object", "$g_presentation_obj_3"),
              (val_sub, ":random_no", "$g_presentation_obj_3_val"),
              (val_sub, ":random_no", 10),
              (lt, ":random_no", 0),
              (is_between, ":obj_3_new_val", ":between_1", ":between_2"),
              (val_add, ":obj_3_new_val", ":adder"),
            (else_try),
              (is_between, ":obj_4_new_val", ":between_1", ":between_2"),
              (val_add, ":obj_4_new_val", ":adder"),
            (else_try),
              (val_add, ":difference", 1),
            (try_end),
          (try_end),
          (assign, "$g_presentation_obj_1_val", ":obj_1_new_val"),
          (assign, "$g_presentation_obj_2_val", ":obj_2_new_val"),
          (assign, "$g_presentation_obj_3_val", ":obj_3_new_val"),
          (assign, "$g_presentation_obj_4_val", ":obj_4_new_val"),
        (try_end),
        (overlay_set_val, "$g_presentation_obj_1", "$g_presentation_obj_1_val"),
        (overlay_set_val, "$g_presentation_obj_2", "$g_presentation_obj_2_val"),
        (overlay_set_val, "$g_presentation_obj_3", "$g_presentation_obj_3_val"),
        (overlay_set_val, "$g_presentation_obj_4", "$g_presentation_obj_4_val"),
        (assign, reg1, "$g_presentation_obj_1_val"),
        (str_store_string, s1, "str_reg1"),
        (overlay_set_text, "$g_presentation_obj_5", s1),
        (assign, reg1, "$g_presentation_obj_2_val"),
        (str_store_string, s1, "str_reg1"),
        (overlay_set_text, "$g_presentation_obj_6", s1),
        (assign, reg1, "$g_presentation_obj_3_val"),
        (str_store_string, s1, "str_reg1"),
        (overlay_set_text, "$g_presentation_obj_7", s1),
        (assign, reg1, "$g_presentation_obj_4_val"),
        (str_store_string, s1, "str_reg1"),
        (overlay_set_text, "$g_presentation_obj_8", s1),
        ]),
      ]),

  ("arena_training", prsntf_read_only, 0, [
      (ti_on_presentation_load,
       [(presentation_set_duration, 999999),
        (set_fixed_point_multiplier, 1000),
        (get_player_agent_no, ":player_agent"),
        (agent_get_kill_count, reg1, ":player_agent", 1),
        (str_store_string, s1, "@Number of men knocked down: {reg1}"),
        (create_text_overlay, "$g_presentation_obj_1", s1),
        (overlay_set_color, "$g_presentation_obj_1", 0xFFFFFF),
        (position_set_x, pos1, 10),
        (position_set_y, pos1, 700),
        (overlay_set_position, "$g_presentation_obj_1", pos1),
        (assign, reg1, 0),
        (str_store_string, s1, "@Number of men left: {reg1}"),
        (create_text_overlay, "$g_presentation_obj_2", s1),
        (overlay_set_color, "$g_presentation_obj_2", 0xFFFFFF),
        (position_set_x, pos1, 10),
        (position_set_y, pos1, 670),
        (overlay_set_position, "$g_presentation_obj_2", pos1),
        ]),
      (ti_on_presentation_run,
       [(get_player_agent_no, ":player_agent"),
        (agent_get_kill_count, reg1, ":player_agent", 1),
        (str_store_string, s1, "@Opponents Beaten: {reg1}"),
        (overlay_set_text, "$g_presentation_obj_1", s1),
        (assign, ":num_left", "$g_arena_training_max_opponents"),
        (try_for_agents, ":agent_no"),
          (agent_is_human, ":agent_no"),
          (neg|agent_is_alive, ":agent_no"),
          (neq, ":agent_no", ":player_agent"),
          (val_sub, ":num_left", 1),
        (try_end),
        (assign, reg1, ":num_left"),
        (str_store_string, s1, "@Opponents Remaining: {reg1}"),
        (overlay_set_text, "$g_presentation_obj_2", s1),
        ]),
      ]),

  ("retirement", 0, mesh_load_window,
   [
     (ti_on_presentation_load,
      [
        (presentation_set_duration, 999999),
        (set_fixed_point_multiplier, 1000),
        (create_button_overlay, "$g_presentation_obj_1", "@Remain in retirement.", tf_center_justify),
        (create_button_overlay, "$g_presentation_obj_2", "@Go back to the adventuring.", tf_center_justify),
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 80),
        (overlay_set_position, "$g_presentation_obj_1", pos1),
        (position_set_y, pos1, 40),
        (overlay_set_position, "$g_presentation_obj_2", pos1),
        (assign, ":total_effect", 0),
        #wealth
        (store_troop_gold, ":wealth", "trp_player"),
        (store_div, ":wealth_effect", ":wealth", 200),
        (val_add, ":total_effect", ":wealth_effect"),
        #ruled centers and friendly/enemy centers
        (assign, ":num_towns", 0),
        (assign, ":num_castles", 0),
        (assign, ":num_villages", 0),
        (assign, ":num_friendly_towns", 0),
        (assign, ":num_friendly_villages", 0),
        (assign, ":num_enemy_towns", 0),
        (assign, ":num_enemy_villages", 0),
        (try_for_range, ":center_no", centers_begin, centers_end),
          (party_get_slot, ":type", ":center_no", slot_party_type),
          (try_begin),
            (party_slot_eq, ":center_no", slot_town_lord, "trp_player"),
            (try_begin),
              (eq, ":type", spt_town),
              (val_add, ":num_towns", 1),
            (else_try),
              (eq, ":type", spt_castle),
              (val_add, ":num_castles", 1),
            (else_try),
              (val_add, ":num_villages", 1),
            (try_end),
          (try_end),
          (party_get_slot, ":relation", ":center_no", slot_center_player_relation),
          (try_begin),
            (ge, ":relation", 40),
            (try_begin),
              (eq, ":type", spt_town),
              (val_add, ":num_friendly_towns", 1),
            (else_try),
              (eq, ":type", spt_village),
              (val_add, ":num_friendly_villages", 1),
            (try_end),
          (else_try),
            (le, ":relation", -40),
            (try_begin),
              (eq, ":type", spt_town),
              (val_add, ":num_enemy_towns", 1),
            (else_try),
              (eq, ":type", spt_village),
              (val_add, ":num_enemy_villages", 1),
            (try_end),
          (try_end),
        (try_end),
        (store_mul, ":num_towns_effect", ":num_towns", 100),
        (store_mul, ":num_castles_effect", ":num_castles", 50),
        (store_mul, ":num_villages_effect", ":num_villages", 20),
        (store_add, ":num_centers", ":num_towns", ":num_castles"),
        (val_add, ":num_centers", ":num_villages"),
        (store_add, ":ruled_centers_effect", ":num_towns_effect", ":num_castles_effect"),
        (val_add, ":ruled_centers_effect", ":num_villages_effect"),
        (val_add, ":total_effect", ":ruled_centers_effect"),
        (store_mul, ":num_friendly_towns_effect", ":num_friendly_towns", 20),
        (store_mul, ":num_friendly_villages_effect", ":num_friendly_villages", 4),
        (store_add, ":num_friendly_centers", ":num_friendly_towns", ":num_friendly_villages"),
        (store_add, ":friendly_centers_effect", ":num_friendly_towns_effect", ":num_friendly_villages_effect"),
        (val_add, ":total_effect", ":friendly_centers_effect"),
        (store_mul, ":num_enemy_towns_effect", ":num_enemy_towns", -40),
        (store_mul, ":num_enemy_villages_effect", ":num_enemy_villages", -8),
        (store_add, ":num_enemy_centers", ":num_enemy_towns", ":num_enemy_villages"),
        (store_add, ":enemy_centers_effect", ":num_enemy_towns_effect", ":num_enemy_villages_effect"),
        (val_add, ":total_effect", ":enemy_centers_effect"),
        #inventory
        (assign, ":total_item_value", 0),
        (troop_get_inventory_capacity, ":capacity", "trp_player"),
        (try_for_range, ":i_inv", 0, ":capacity"),
          (troop_get_inventory_slot, ":item", "trp_player", ":i_inv"),
          (ge, ":item", 0),
          (store_item_value, ":item_value", ":item"),
          (val_add, ":total_item_value", ":item_value"),
        (try_end),
        (store_div, ":total_item_value_effect", ":total_item_value", 200),
        (val_add, ":total_effect", ":total_item_value_effect"),
        #renown
        (troop_get_slot, ":renown", "trp_player", slot_troop_renown),
        (store_div, ":renown_effect", ":renown", 1),
        (val_add, ":total_effect", ":renown_effect"),
        #enemies and friends
        (assign, ":num_enemies", 0),
        (assign, ":num_friends", 0),
        (try_for_range, ":troop_no", heroes_begin, heroes_end),
          (this_or_next|troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_hero),
          (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_kingdom_lady),
          (call_script, "script_troop_get_player_relation", ":troop_no"),
          (assign, ":relation", reg0),
          #(troop_get_slot, ":relation", ":troop_no", slot_troop_player_relation),
          (try_begin),
            (ge, ":relation", 40),
            (val_add, ":num_friends", 1),
          (else_try),
            (le, ":relation", -40),
            (val_add, ":num_enemies", 1),
          (try_end),
        (try_end),
        (store_mul, ":num_friends_effect", ":num_friends", 10),
        (val_add, ":total_effect", ":num_friends_effect"),
        (store_mul, ":num_enemies_effect", ":num_enemies", -10),
        (val_add, ":total_effect", ":num_enemies_effect"),
        #current day
        (store_current_day, ":cur_day"),
        (store_div, ":cur_day_effect", ":cur_day", -1),
        (val_add, ":total_effect", ":cur_day_effect"),
        #g_total_victories
        (store_mul, ":total_victories_effect", "$g_total_victories", 2),
        (val_add, ":total_effect", ":total_victories_effect"),
        #g_total_defeats
        (store_mul, ":total_defeats_effect", "$g_total_defeats", -3),
        (val_add, ":total_effect", ":total_defeats_effect"),
        #g_total_quests_completed
        (store_mul, ":total_quests_completed_effect", "$g_total_quests_completed", 4),
        (val_add, ":total_effect", ":total_quests_completed_effect"),
        #xp
        (troop_get_xp, ":xp", "trp_player"),
        (store_div, ":xp_effect", ":xp", 5000),
        (val_add, ":total_effect", ":xp_effect"),
        #companions joined / departed
        (assign, ":num_companions_joined", 0),
        (assign, ":num_companions_departed", 0),
        (try_for_range, ":troop_no", companions_begin, companions_end),
          (try_begin),
            (troop_slot_eq, ":troop_no", slot_troop_occupation, slto_player_companion),
            (val_add, ":num_companions_joined", 1),
          (else_try),
            (troop_slot_ge, ":troop_no", slot_troop_playerparty_history, 1),
            (val_add, ":num_companions_departed", 1),
          (try_end),
        (try_end),
        (store_div, ":num_companions_joined_effect", ":num_companions_joined", 2),
        (val_add, ":total_effect", ":num_companions_joined_effect"),
        (store_div, ":num_companions_departed_effect", ":num_companions_departed", -4),
        (val_add, ":total_effect", ":num_companions_departed_effect"),
        #difficulty
        (get_average_game_difficulty, ":difficulty"),
        (assign, ":difficulty_effect", ":total_effect"),
        (val_mul, ":total_effect", ":difficulty"),
        (val_div, ":total_effect", 75),
        (val_mul, ":total_effect", ":difficulty"),
        (val_div, ":total_effect", 75),
        (store_sub, ":difficulty_effect", ":total_effect", ":difficulty_effect"),

        (assign, reg5, ":cur_day"),
        (store_character_level, reg4, "trp_player"),
        (create_text_overlay, reg1, "@You have retired at level {reg4} after {reg5} days of adventuring.", tf_center_justify),
        (position_set_x, pos1, 700),
        (position_set_y, pos1, 700),
        (overlay_set_position, reg1, pos1),
        (position_set_x, pos1, 950),
        (position_set_y, pos1, 950),
        (overlay_set_size, reg1, pos1),
##        (overlay_set_color, reg1, 0),
        (create_text_overlay, reg2, "@Effect on Score", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg2, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 670),
        (overlay_set_position, reg2, pos1),
        (assign, reg0, ":num_centers"),
        (create_text_overlay, reg1, "@Settlements owned by you: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 650),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":ruled_centers_effect"),
        (create_text_overlay, reg1, "@{reg0?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 650),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (try_end),

        (assign, reg0, ":num_friendly_centers"),
        (create_text_overlay, reg1, "@Friendly Settlements: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 630),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":friendly_centers_effect"),
        (create_text_overlay, reg1, "@{reg0?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 630),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (try_end),

        (assign, reg0, ":num_enemy_centers"),
        (create_text_overlay, reg1, "@Hostile Settlements: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 610),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":enemy_centers_effect"),
        (create_text_overlay, reg1, "@{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 610),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (lt, reg0, 0),
          (overlay_set_color, reg1, 0xFF0000),
        (try_end),

        (assign, reg0, ":num_friends"),
        (create_text_overlay, reg1, "@Friendly Lords: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 590),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":num_friends_effect"),
        (create_text_overlay, reg1, "@{reg0?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 590),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (try_end),

        (assign, reg0, ":num_enemies"),
        (create_text_overlay, reg1, "@Enemy Lords: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 570),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":num_enemies_effect"),
        (create_text_overlay, reg1, "@{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 570),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (lt, reg0, 0),
          (overlay_set_color, reg1, 0xFF0000),
        (try_end),

        (assign, reg0, "$g_total_victories"),
        (create_text_overlay, reg1, "@Victories: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 550),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":total_victories_effect"),
        (create_text_overlay, reg1, "@{reg0?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 550),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (try_end),

        (assign, reg0, "$g_total_defeats"),
        (create_text_overlay, reg1, "@Defeats: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 530),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":total_defeats_effect"),
        (create_text_overlay, reg1, "@{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 530),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (lt, reg0, 0),
          (overlay_set_color, reg1, 0xFF0000),
        (try_end),

        (assign, reg0, "$g_total_quests_completed"),
        (create_text_overlay, reg1, "@Quests Completed: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 510),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":total_quests_completed_effect"),
        (create_text_overlay, reg1, "@{reg0?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 510),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (try_end),

        (assign, reg0, ":num_companions_joined"),
        (create_text_overlay, reg1, "@Companions Found: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 490),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":num_companions_joined_effect"),
        (create_text_overlay, reg1, "@{reg0?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 490),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (try_end),

        (assign, reg0, ":num_companions_departed"),
        (create_text_overlay, reg1, "@Companions Lost/Departed: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 470),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":num_companions_departed_effect"),
        (create_text_overlay, reg1, "@{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 470),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (lt, reg0, 0),
          (overlay_set_color, reg1, 0xFF0000),
        (try_end),

        (assign, reg0, ":wealth"),
        (create_text_overlay, reg1, "@Wealth: {reg0} denars", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 450),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":wealth_effect"),
        (create_text_overlay, reg1, "@{reg0?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 450),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (try_end),

        (assign, reg0, ":total_item_value"),
        (create_text_overlay, reg1, "@Inventory: {reg0} denars", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 430),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":total_item_value_effect"),
        (create_text_overlay, reg1, "@{reg0?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 430),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (try_end),

        (assign, reg0, ":renown"),
        (create_text_overlay, reg1, "@Renown: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 410),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":renown_effect"),
        (create_text_overlay, reg1, "@{reg0?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 410),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (try_end),

        (assign, reg0, ":xp"),
        (create_text_overlay, reg1, "@Experience: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 390),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":xp_effect"),
        (create_text_overlay, reg1, "@{reg0?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 390),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (try_end),

        (assign, reg0, ":cur_day"),
        (create_text_overlay, reg1, "@Days Passed: {reg0}", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 370),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":cur_day_effect"),
        (create_text_overlay, reg1, "@{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 370),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (lt, reg0, 0),
          (overlay_set_color, reg1, 0xFF0000),
        (try_end),

        (assign, reg0, ":difficulty"),
        (create_text_overlay, reg1, "@Difficulty: {reg0}%", 0),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 350),
        (overlay_set_position, reg1, pos1),
        (assign, reg0, ":difficulty_effect"),
        (assign, reg3, reg0),
        (val_max, reg3, 0),
        (create_text_overlay, reg1, "@{reg3?+:}{reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        (overlay_set_size, reg1, pos1),
        (position_set_x, pos1, 850),
        (position_set_y, pos1, 350),
        (overlay_set_position, reg1, pos1),
        (try_begin),
          (gt, reg0, 0),
          (overlay_set_color, reg1, 0x00FF00),
        (else_try),
          (lt, reg0, 0),
          (overlay_set_color, reg1, 0xFF0000),
        (try_end),

        (assign, reg0, ":total_effect"),
        (create_text_overlay, reg1, "@TOTAL SCORE: {reg0}", tf_center_justify),
        (position_set_x, pos1, 750),
        (position_set_y, pos1, 320),
        (overlay_set_position, reg1, pos1),

        (assign, ":total_villages", 0),
        (assign, ":total_castles", 0),
        (assign, ":total_towns", 0),
        (assign, ":total_village_rels", 0),
        (assign, ":total_castle_rels", 0),
        (assign, ":total_town_rels", 0),
        (try_for_range, ":cur_center_no", centers_begin, centers_end),
          (try_begin),
            (party_slot_eq, ":cur_center_no", slot_town_lord, "trp_player"),
            (try_begin),
              (party_slot_eq, ":cur_center_no", slot_party_type, spt_town),
              (val_add, ":total_towns", 1),
            (else_try),
              (party_slot_eq, ":cur_center_no", slot_party_type, spt_castle),
              (val_add, ":total_castles", 1),
            (else_try),
              (val_add, ":total_villages", 1),
            (try_end),
          (else_try),
            (party_get_slot, ":center_relation", ":cur_center_no", slot_center_player_relation),
            (gt, ":center_relation", 0),
            (try_begin),
              (party_slot_eq, ":cur_center_no", slot_party_type, spt_town),
              (val_add, ":total_town_rels", ":center_relation"),
            (else_try),
              (party_slot_eq, ":cur_center_no", slot_party_type, spt_castle),
              (val_add, ":total_castle_rels", ":center_relation"),
            (else_try),
              (val_add, ":total_village_rels", ":center_relation"),
            (try_end),
          (try_end),
        (try_end),
        (try_begin),
          (gt, ":total_towns", 0),
          (store_random_in_range, ":random_town", 0, ":total_towns"),
          (try_for_range, ":cur_center_no", towns_begin, towns_end),
            (party_slot_eq, ":cur_center_no", slot_town_lord, "trp_player"),
            (val_sub, ":random_town", 1),
            (lt, ":random_town", 0),
            (str_store_party_name, s9, ":cur_center_no"),
          (try_end),
        (else_try),
          (gt, ":total_town_rels", 0),
          (store_random_in_range, ":random_town_rel", 0, ":total_town_rels"),
          (try_for_range, ":cur_center_no", towns_begin, towns_end),
            (party_get_slot, ":center_relation", ":cur_center_no", slot_center_player_relation),
            (gt, ":center_relation", 0),
            (val_sub, ":random_town_rel", ":center_relation"),
            (lt, ":random_town_rel", 0),
            (str_store_party_name, s9, ":cur_center_no"),
          (try_end),
        (else_try),
          (store_random_in_range, ":random_town", towns_begin, towns_end),
          (str_store_party_name, s9, ":random_town"),
        (try_end),
        (try_begin),
          (gt, ":total_castles", 0),
          (store_random_in_range, ":random_castle", 0, ":total_castles"),
          (try_for_range, ":cur_center_no", castles_begin, castles_end),
            (party_slot_eq, ":cur_center_no", slot_town_lord, "trp_player"),
            (val_sub, ":random_castle", 1),
            (lt, ":random_castle", 0),
            (str_store_party_name, s8, ":cur_center_no"),
          (try_end),
        (else_try),
          (gt, ":total_castle_rels", 0),
          (store_random_in_range, ":random_castle_rel", 0, ":total_castle_rels"),
          (try_for_range, ":cur_center_no", castles_begin, castles_end),
            (party_get_slot, ":center_relation", ":cur_center_no", slot_center_player_relation),
            (gt, ":center_relation", 0),
            (val_sub, ":random_castle_rel", ":center_relation"),
            (lt, ":random_castle_rel", 0),
            (str_store_party_name, s8, ":cur_center_no"),
          (try_end),
        (else_try),
          (store_random_in_range, ":random_castle", castles_begin, castles_end),
          (str_store_party_name, s8, ":random_castle"),
        (try_end),
        (try_begin),
          (gt, ":total_villages", 0),
          (store_random_in_range, ":random_village", 0, ":total_villages"),
          (try_for_range, ":cur_center_no", villages_begin, villages_end),
            (party_slot_eq, ":cur_center_no", slot_town_lord, "trp_player"),
            (val_sub, ":random_village", 1),
            (lt, ":random_village", 0),
            (str_store_party_name, s7, ":cur_center_no"),
          (try_end),
        (else_try),
          (gt, ":total_village_rels", 0),
          (store_random_in_range, ":random_village_rel", 0, ":total_village_rels"),
          (try_for_range, ":cur_center_no", villages_begin, villages_end),
            (party_get_slot, ":center_relation", ":cur_center_no", slot_center_player_relation),
            (gt, ":center_relation", 0),
            (val_sub, ":random_village_rel", ":center_relation"),
            (lt, ":random_village_rel", 0),
            (str_store_party_name, s7, ":cur_center_no"),
          (try_end),
        (else_try),
          (store_random_in_range, ":random_village", villages_begin, villages_end),
          (str_store_party_name, s7, ":random_village"),
        (try_end),

        (try_begin),
          (lt, ":total_effect", 100),
          (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_retirement_troop", 0),
          (str_store_string, s0, "str_retirement_text_1"),
        (else_try),
          (lt, ":total_effect", 200),
          (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_retirement_troop", 1),
          (str_store_string, s0, "str_retirement_text_2"),
        (else_try),
          (lt, ":total_effect", 400),
          (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_retirement_troop", 2),
          (str_store_string, s0, "str_retirement_text_3"),
        (else_try),
          (lt, ":total_effect", 700),
          (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_retirement_troop", 3),
          (str_store_string, s0, "str_retirement_text_4"),
        (else_try),
          (lt, ":total_effect", 1200),
          (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_retirement_troop", 4),
          (str_store_string, s0, "str_retirement_text_5"),
        (else_try),
          (lt, ":total_effect", 1850),
          (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_retirement_troop", 5),
          (str_store_string, s0, "str_retirement_text_6"),
        (else_try),
          (lt, ":total_effect", 2500),
          (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_retirement_troop", 6),
          (str_store_string, s0, "str_retirement_text_7"),
        (else_try),
          (lt, ":total_effect", 3500),
          (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_retirement_troop", 7),
          (str_store_string, s0, "str_retirement_text_8"),
        (else_try),
          (lt, ":total_effect", 5000),
          (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_retirement_troop", 8),
          (str_store_string, s0, "str_retirement_text_9"),
        (else_try),
          (create_mesh_overlay_with_tableau_material, reg1, -1, "tableau_retirement_troop", 9),
          (str_store_string, s0, "str_retirement_text_10"),
        (try_end),
        (position_set_x, pos1, 0),
        (position_set_y, pos1, 120),
        (overlay_set_position, reg1, pos1),

        (create_text_overlay, reg1, s0, tf_double_space|tf_scrollable),
        (position_set_x, pos1, 600),
        (position_set_y, pos1, 120),
        (overlay_set_position, reg1, pos1),
        (position_set_x, pos1, 360),
        (position_set_y, pos1, 190),
        (overlay_set_area_size, reg1, pos1),
        (position_set_x, pos1, 800),
        (position_set_y, pos1, 800),
        (overlay_set_size, reg1, pos1),
        ]),
     (ti_on_presentation_event_state_change,
      [
        (store_trigger_param_1, ":object"),
        (try_begin),
          (eq, ":object", "$g_presentation_obj_1"),
          (jump_to_menu, "mnu_end_game"),
          (start_presentation, "prsnt_game_credits"),
        (else_try),
          (eq, ":object", "$g_presentation_obj_2"),
          (presentation_set_duration, 0),
          (change_screen_return),
        (try_end),
        ]),
     ]),
  
  ###########################
  # Form Ranks Kit section
  #
  
  # Key configuration
  ("key_config", 0, mesh_load_window, [
            (ti_on_presentation_load, [
                    (presentation_set_duration, 999999),
                    (set_fixed_point_multiplier, 1000),
                    ]+[
                    (assign, "$g_presentation_obj_"+str(x+1), -1) \
                        for x in range(0, 2*len(formation_keys_list))
                    ]+[
                    (assign, "$formation_key_config_back", -1),
                    (assign, "$formation_key_config_reset", -1),
                    (assign, "$formation_key_config_default", -1),
                    (assign, "$formation_key_config_title", -1),
                    (assign, "$key_config_button_down", 0),
                    (set_spawn_radius, 5),
                    (spawn_around_party, "p_main_party", "pt_none"),
                    (assign, "$formation_key_config_vector", reg0),
                    (disable_party, reg0),
                    (spawn_around_party, "p_main_party", "pt_none"),
                    (assign, "$formation_key_config_vector_back", reg0),
                    (disable_party, reg0),
                    (try_for_range, ":slot", 0, 0xff),
                        (party_set_slot, "$formation_key_config_vector", ":slot", -1),
                        (party_set_slot, "$formation_key_config_vector_back", ":slot", -1),                        
                    (try_end),
                    (try_for_range, ":key", 0, len(formation_keys_list)),
                        (store_add, ":slot", slot_party_formation_keys_begin, ":key"),
                        (party_get_slot, ":mapped_key", "p_main_party", ":slot"),
                        (neq, ":mapped_key", 0xff),
                        (party_set_slot, "$formation_key_config_vector", ":mapped_key", ":key"),
                        (party_set_slot, "$formation_key_config_vector_back", ":mapped_key", ":key"),
                    (try_end),
                    (init_position, pos1),
                    (position_set_y, pos1, 680),
                    (position_set_x, pos1, 200),
                    (create_text_overlay, "$formation_key_config_title", "@Formation Keys Configuration", tf_center_justify),
                    (overlay_set_position, "$formation_key_config_title", pos1),
                    (position_set_y, pos1, 150),
                    (position_set_x, pos1, 600),
                    (create_text_overlay, "$formation_key_config_notes", "@Press Esc to^^Disable a key", tf_left_align),
                    (overlay_set_position, "$formation_key_config_notes", pos1),
                    (overlay_set_color, "$formation_key_config_notes", 0x600000),
                    (overlay_set_alpha, "$formation_key_config_notes", 0x00),
                    (position_set_y, pos1, 600),
                    (position_set_x, pos1, 25),
                    (position_set_x, pos2, 1000),
                    (position_set_y, pos2, 1000),
                    (position_set_x, pos3, 120),
                    (position_set_y, pos3, 40),
                    ]+concatenate_scripts([
                     [(store_add, ":key_name", formation_key_names_begin, y),
                      (create_text_overlay, "$g_presentation_obj_"+str(y*2+1), ":key_name", tf_left_align),
                      (overlay_set_position, "$g_presentation_obj_"+str(y*2+1), pos1),
                      (overlay_set_size, "$g_presentation_obj_"+str(y*2+1), pos2),
                      (position_move_x, pos1, 25),
                      (create_game_button_overlay, "$g_presentation_obj_"+str(y*2+2), "str_no_string", tf_center_justify),
                      (overlay_set_position, "$g_presentation_obj_"+str(y*2+2), pos1),
                      (overlay_set_size, "$g_presentation_obj_"+str(y*2+2), pos3),
                      (position_move_x, pos1, -25),
                      (position_move_y, pos1, -4),
                      (try_begin),
                        (eq, len(formation_keys_list)/3+3, y),
                        (position_move_y, pos1, 4*(y+1)),
                        (position_move_x, pos1, 32),
                      (try_end),
                      (try_begin),
                        (eq, len(formation_keys_list)/3+3+(len(formation_keys_list)-len(formation_keys_list)/3-4)/2, y),
                        (position_move_y, pos1, 4*(y-len(formation_keys_list)/3-3)),
                        (position_move_x, pos1, 32),
                      (try_end),
                      (try_begin),
                      ] for y in range(0, len(formation_keys_list)) ])+[
                    (call_script, "script_update_formation_key_config_buttons"),
                    (position_set_y, pos1, 25),
                    (position_set_x, pos1, 900),
                    (create_game_button_overlay, "$formation_key_config_back", "@Back", tf_center_justify),
                    (overlay_set_position, "$formation_key_config_back", pos1),
                    (position_move_x, pos1, -16),
                    (create_game_button_overlay, "$formation_key_config_reset", "@Reset", tf_center_justify),
                    (overlay_set_position, "$formation_key_config_reset", pos1),
                    (position_move_x, pos1, -16),
                    (create_game_button_overlay, "$formation_key_config_default", "@Default", tf_center_justify),
                    (overlay_set_position, "$formation_key_config_default", pos1),
                    ]
             ),

            (ti_on_presentation_event_state_change, [
                    (store_trigger_param_1, ":object"),
                    #(store_trigger_param_2, ":value"),
                    (try_begin),
                    ]+concatenate_scripts([
                        [
                        (eq, ":object", "$g_presentation_obj_"+str(y*2+2)),
                        (assign, "$key_config_button_down", 1),
                        (assign, "$key_config_listener", "$g_presentation_obj_"+str(y*2+2)),
                        (overlay_set_text, "$key_config_listener", "@Press a key", tf_center_justify),
                        (overlay_set_color, "$key_config_listener", 0x0000FF),
                        (overlay_set_hilight_color, "$key_config_listener", 0x0000FF),
                        (assign, "$formation_key_no", y),
                    (else_try),] for y in range(0, len(formation_keys_list)) 
                        ])+[
                        (eq, ":object", "$formation_key_config_back"),
                        (eq, "$key_config_button_down", 0),
                        (presentation_set_duration, 0),
                        (jump_to_menu, "mnu_camp_action"),
                    (else_try),
                        (eq, ":object", "$formation_key_config_reset"),
                        (try_for_range, ":slot", slot_party_formation_keys_begin, slot_party_formation_keys_begin+len(formation_keys_list)),
                                (party_set_slot, "p_main_party", ":slot", 0xff),
                        (try_end),
                        (try_for_range, ":slot", 0, 0xff),
                                (party_get_slot, ":key", "$formation_key_config_vector_back", ":slot"),
                                (party_set_slot, "$formation_key_config_vector", ":slot", ":key"),
                                (neq, ":key", -1),
                                (val_add, ":key", slot_party_formation_keys_begin),
                                (party_set_slot, "p_main_party", ":key", ":slot"),
                        (try_end),
                        (call_script, "script_update_formation_key_config_buttons"),
                    (else_try),
                        (eq, ":object", "$formation_key_config_default"),
                        ]+[
                        (party_set_slot, "p_main_party", slot_party_formation_keys_begin+x, formation_default_key_list[x]) \
                            for x in range(0, len(formation_keys_list))
                    ]+[
                        (try_for_range, ":slot", 0, 0xff),
                                (party_set_slot, "$formation_key_config_vector", ":slot", -1),
                        (try_end),
                        (try_for_range, ":key", 0, len(formation_keys_list)),
                                (store_add, ":slot", ":key", slot_party_formation_keys_begin),
                                (party_get_slot, ":mapped_key", "p_main_party", ":slot"),
                                (neq, ":mapped_key", 0xff),
                                (party_set_slot, "$formation_key_config_vector", ":mapped_key", ":key"),
                        (try_end),
                        (call_script, "script_update_formation_key_config_buttons"),
                    (try_end),
                    (try_begin),
                        (eq, "$key_config_button_down", 1),
                        (overlay_set_alpha, "$formation_key_config_notes", 0xFF),
                    (try_end),
                    ]
             ),

            (ti_on_presentation_run, [
                    (eq, "$key_config_button_down", 1),
                    (store_add, ":slot", "$formation_key_no", slot_party_formation_keys_begin),
                    (assign, "$key_config_button_down", 0),
                    (try_begin),
                    ]+concatenate_scripts([
                        [
                            (key_clicked, all_keys_list[x][0]),
                            #(overlay_set_text, "$key_config_listener", all_keys_list[x][1], tf_center_justify),
                            #(overlay_set_color, "$key_config_listener", 0x0),
                            (party_get_slot, ":prev_mapped_key", "p_main_party", ":slot"),
                            (party_set_slot, "$formation_key_config_vector", ":prev_mapped_key", -1),
                            (party_set_slot, "p_main_party", ":slot", all_keys_list[x][0]),
                            (try_begin),
                                (party_get_slot, ":prev_key", "$formation_key_config_vector", all_keys_list[x][0]),
                                (party_set_slot, "$formation_key_config_vector", all_keys_list[x][0], "$formation_key_no"),
                                (neq, ":prev_key", -1),
                                (store_add, ":prev_slot", ":prev_key", slot_party_formation_keys_begin),
                                (party_set_slot, "p_main_party", ":prev_slot", 0xff),
                            (try_end),
                            (else_try),
                            ] for x in range(0, len(all_keys_list))
                        ])+[
                        (key_clicked, key_escape),
                        #(overlay_set_text, "$key_config_listener", "@Undefined"),
                        #(overlay_set_color, "$key_config_listener", 0xFF0000),
                        (party_get_slot, ":prev_mapped_key", "p_main_party", ":slot"),
                        (party_set_slot, "$formation_key_config_vector", ":prev_mapped_key", -1),
                        (party_set_slot, "p_main_party", ":slot", 0xff),
                    (else_try),
                        (assign, "$key_config_button_down", 1),
                    (try_end),
                    (try_begin),
                        (eq, "$key_config_button_down", 0),
                        (overlay_set_hilight_color, "$key_config_listener", 0x00FF00),
                        (overlay_set_alpha, "$formation_key_config_notes", 0x00),
                        (call_script, "script_update_formation_key_config_buttons"),
                    (try_end),
                    ]
             ),
            ]
   ),

  ("battle_formations",0,0,[
      (ti_on_presentation_load,
       [(set_fixed_point_multiplier, 1000),
        (assign, "$g_presentation_battle_formations_active", 1),

        (str_clear, s7),

        (try_for_agents, ":agent_no"),
          (agent_set_slot, ":agent_no", slot_agent_map_overlay_id, 0),
        (try_end),

        (set_fixed_point_multiplier, 1000),
        (position_set_x, pos1, 0),
        (position_set_y, pos1, 650),
        (position_set_x, pos2, 32500),
        (position_set_y, pos2, 2500),
        (position_set_x, pos3, 20),
        (position_set_y, pos3, 663),
        (position_set_x, pos4, 50),
        (position_set_y, pos4, 663),
        (position_set_y, pos5, 663),
        (position_set_x, pos6, 900),
        (position_set_y, pos6, 900),
        ]+concatenate_scripts([
            [(create_image_button_overlay, "$g_presentation_obj_"+str(x+1), "mesh_white_plane", "mesh_white_plane"),
            (position_set_y, pos1, 650-50*x),
            (overlay_set_position, "$g_presentation_obj_"+str(x+1), pos1),
            (overlay_set_size, "$g_presentation_obj_"+str(x+1), pos2),
            (overlay_set_alpha, "$g_presentation_obj_"+str(x+1), 0),
            (overlay_set_color, "$g_presentation_obj_"+str(x+1), 0xFFFF00),
            (create_check_box_overlay, "$g_presentation_obj_"+str(x+1+formation_arrays), "mesh_checkbox_off", "mesh_checkbox_on"),
            (position_set_y, pos3, 663-50*x),
            (overlay_set_position, "$g_presentation_obj_"+str(x+1+formation_arrays), pos3),
            (create_text_overlay, "$g_presentation_obj_"+str(x+1+2*formation_arrays), s7, 0),
            (position_set_y, pos4, 663-50*x),
            (overlay_set_position, "$g_presentation_obj_"+str(x+1+2*formation_arrays), pos4),
            (position_set_y, pos5, 663-50*x),
            (position_set_x, pos5, 250),
            (create_text_overlay, "$g_presentation_array_formation_"+str(x+1), s7, tf_center_justify),
            (overlay_set_position, "$g_presentation_array_formation_"+str(x+1), pos5),
            (overlay_set_size, "$g_presentation_array_formation_"+str(x+1), pos6),
            (position_set_x, pos5, 375),
            (create_text_overlay, "$g_presentation_array_movement_"+str(x+1), s7, tf_center_justify),
            (overlay_set_position, "$g_presentation_array_movement_"+str(x+1), pos5),
            (overlay_set_size, "$g_presentation_array_movement_"+str(x+1), pos6),
            (position_set_x, pos5, 500),
            (create_text_overlay, "$g_presentation_array_density_"+str(x+1), s7, tf_center_justify),
            (overlay_set_position, "$g_presentation_array_density_"+str(x+1), pos5),
            (overlay_set_size, "$g_presentation_array_density_"+str(x+1), pos6),
            (position_set_x, pos5, 600),
            (create_text_overlay, "$g_presentation_array_facing_"+str(x+1), s7, tf_center_justify),
            (overlay_set_position, "$g_presentation_array_facing_"+str(x+1), pos5),
            (overlay_set_size, "$g_presentation_array_facing_"+str(x+1), pos6),
            ] for x in range(0, formation_arrays)
        ])+[
        

        (get_player_agent_no, ":player_agent"),
        (agent_get_team, ":player_team", ":player_agent"),
        

        (position_set_y, pos1, 700),
        (create_text_overlay, reg1, "@Battles", tf_center_justify),
        (position_set_x, pos1, 100),
        (overlay_set_position, reg1, pos1),
        (create_text_overlay, reg1, "@Formation", tf_center_justify),
        (position_set_x, pos1, 250),
        (overlay_set_position, reg1, pos1),
        (create_text_overlay, reg1, "@Action", tf_center_justify),
        (position_set_x, pos1, 375),
        (overlay_set_position, reg1, pos1),
        (create_text_overlay, reg1, "@Density", tf_center_justify),
        (position_set_x, pos1, 500),
        (overlay_set_position, reg1, pos1),
        (create_text_overlay, reg1, "@Facing", tf_center_justify),
        (position_set_x, pos1, 600),
        (overlay_set_position, reg1, pos1),


        (call_script, "script_update_formation_order_panel", ":player_team"),

        ]+concatenate_scripts([[(store_add, ":formation_pl", formation_commands_pl_begin, x),
           (str_store_string, s1, ":formation_pl"),
           (create_button_overlay, "$g_presentation_obj_"+str(x+1+3*formation_arrays), "@Form {s1}", tf_center_justify)]\
               for x in range(0, formation_types)
           ])+[
        (create_button_overlay, "$g_presentation_obj_"+str(x+1+3*formation_arrays+formation_types), formation_order_functions[x][0], tf_center_justify)\
            for x in range(0, len(formation_order_functions))
        ]+[

        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        ]+[
        (overlay_set_size, "$g_presentation_obj_"+str(x+1+3*formation_arrays), pos1)\
             for x in range(0, formation_types)
        ]+[
        (overlay_set_size, "$g_presentation_obj_"+str(x+1+3*formation_arrays+formation_types), pos1)\
            for x in range(0, len(formation_order_functions))
        ]+[
        (position_set_x, pos1, 250),
        (position_set_y, pos1, 250),
        ]+concatenate_scripts([
        [
        (position_set_y, pos1, 250-20*x),
        (overlay_set_position, "$g_presentation_obj_"+str(x+1+3*formation_arrays), pos1),
        ] for x in range(0, formation_types)
        ])+[
        (position_set_y, pos1, 250-20*formation_types),
        (overlay_set_position, "$g_presentation_obj_"+str(1+formation_types+3*formation_arrays), pos1),
        (position_set_x, pos1, 375),
        (position_set_y, pos1, 250),
        ]+concatenate_scripts([
        [ 
        (position_set_y, pos1, 250-20*x),
        (overlay_set_position, "$g_presentation_obj_"+str(x+2+formation_types+3*formation_arrays), pos1),
        ] for x in range(0, 10)
        ])+[
        (position_set_x, pos1, 500),
        (position_set_y, pos1, 250),
        (overlay_set_position, "$g_presentation_obj_"+str(12+formation_types+3*formation_arrays), pos1),
        (position_set_y, pos1, 230),
        (overlay_set_position, "$g_presentation_obj_"+str(13+formation_types+3*formation_arrays), pos1),

        (position_set_x, pos1, 600),
        (position_set_y, pos1, 250),
        (overlay_set_position, "$g_presentation_obj_"+str(14+formation_types+3*formation_arrays), pos1),
        (position_set_y, pos1, 230),
        (overlay_set_position, "$g_presentation_obj_"+str(15+formation_types+3*formation_arrays), pos1),
        
        (get_scene_boundaries, pos2, pos3),
        (position_transform_position_to_local, pos4, pos2, pos3),
        (set_fixed_point_multiplier, 1000),
        (position_get_x, ":map_width", pos4),
        (position_get_y, ":map_height", pos4),
        (set_fixed_point_multiplier, 1000),
        (store_div, ":map_ratio", ":map_height", 100),
        (store_div, ":map_ratio", ":map_width", ":map_ratio"),
        (try_begin),
          (gt, ":map_ratio", 100),
          (assign, "$g_battle_map_width", 300),
          (store_div, "$g_battle_map_scale", ":map_width", "$g_battle_map_width"),
          (store_div, "$g_battle_map_height", ":map_height", "$g_battle_map_scale"),
        (else_try),
          (assign, "$g_battle_map_height", 300),
          (store_div, "$g_battle_map_scale", ":map_height", "$g_battle_map_height"),
          (store_div, "$g_battle_map_width", ":map_width", "$g_battle_map_scale"),
        (try_end),
        
        (create_image_button_overlay, "$g_battle_map_plane", "mesh_white_plane", "mesh_white_plane"),
        (overlay_set_color, "$g_battle_map_plane", 0),
        (store_add, ":map_bordered_width", "$g_battle_map_width", 20),
        (store_add, ":map_bordered_height", "$g_battle_map_height", 20),
        (store_mul, ":map_scale_x", ":map_bordered_width", 50),
        (store_mul, ":map_scale_y", ":map_bordered_height", 50),
        (position_set_x, pos1, ":map_scale_x"),
        (position_set_y, pos1, ":map_scale_y"),
        (overlay_set_size, "$g_battle_map_plane", pos1),
        (store_sub, ":map_pos_x", 990, ":map_bordered_width"),
        (store_sub, ":map_pos_y", 740, ":map_bordered_height"),
        (position_set_x, pos1, ":map_pos_x"),
        (position_set_y, pos1, ":map_pos_y"),
        (overlay_set_position, "$g_battle_map_plane", pos1),
        (overlay_set_alpha, "$g_battle_map_plane", 0x44),

        (position_set_x, pos1, 750),
        (position_set_y, pos1, 750),
        ]+concatenate_scripts([
        [
        (create_text_overlay, "$g_battle_map_array_"+str(x+1)+"_flag", "@"+str(x+1), tf_center_justify),
        (overlay_set_alpha, "$g_battle_map_array_"+str(x+1)+"_flag", 0),
        (overlay_set_size, "$g_battle_map_array_"+str(x+1)+"_flag", pos1),
        (overlay_set_color, "$g_battle_map_array_"+str(x+1)+"_flag", 0xFFFFFF),
        ] for x in range(0, formation_arrays)
        ])+[
        (call_script, "script_update_formation_order_panel_checked_arrays"),
        (call_script, "script_update_formation_order_panel_statistics_and_map"),

        (presentation_set_duration, 999999),
        ]),
      (ti_on_presentation_event_state_change,
       [(store_trigger_param_1, ":object"),
        (store_trigger_param_2, ":value"),
        (get_player_agent_no, ":player_agent"),
        (agent_get_team, ":player_team", ":player_agent"),

        (try_begin),
          ]+concatenate_scripts([
          [
          (eq, ":object", "$g_presentation_obj_"+str(x+1+formation_arrays)),
          #(assign, ":update_listeners", 1),
          (try_begin),
          (lt, x, "$formation_max_array_no"),
          (party_get_slot, ":mark", formation_vector, slot_formation_array_marks_begin+x),
          (val_mul, ":value", ":mark"),
          (val_or, "$formation_array_selected", ":mark"),
          (val_sub, "$formation_array_selected", ":mark"),
          (val_add, "$formation_array_selected", ":value"),
          (try_end),
          #(try_begin),
          #  (eq, ":value", 1),
          #  (overlay_animate_to_alpha, "$g_presentation_obj_"+str(x+1), 250, 0x44),
          #(else_try),
          #  (overlay_animate_to_alpha, "$g_presentation_obj_"+str(x+1), 250, 0),
          #(try_end),
          (call_script, "script_update_formation_order_panel_checked_arrays"),
        (else_try),
          ] for x in range(0, formation_arrays)
          ])+concatenate_scripts([
          [
          (eq, ":object", "$g_presentation_obj_"+str(x+1)),
          (lt, x, "$formation_max_array_no"),
          (party_get_slot, ":mark", formation_vector, slot_formation_array_marks_begin+x),
          (assign, "$formation_array_selected", ":mark"),
          #]+[
          #(overlay_animate_to_alpha, "$g_presentation_obj_"+str(y+1), 250, 0)\
          #    for y in range(0, formation_arrays) if x != y
          #]+[
          #(overlay_animate_to_alpha, "$g_presentation_obj_"+str(x+1), 250, 0x44),
          #(overlay_set_val, "$g_presentation_obj_"+str(x+1+formation_arrays), 1),
          #]+[
          #(overlay_set_val, "$g_presentation_obj_"+str(y+1+formation_arrays), 0)\
          #    for y in range(0, formation_arrays) if x != y
          #]+[
          (call_script, "script_update_formation_order_panel_checked_arrays"),
        (else_try),
          ] for x in range(0, formation_arrays)
          ])+concatenate_scripts([
          [
          (eq, ":object", "$g_presentation_obj_"+str(x+1+formation_arrays*3)),
          (call_script, "script_apply_formation_key_reaction", x),
          (call_script, "script_update_formation_order_panel", ":player_team"),
        (else_try),
          ] for x in range(0, formation_types)
          ])+concatenate_scripts([
          [
          (eq, ":object", "$g_presentation_obj_"+str(x+1+formation_types+formation_arrays*3)),
          formation_order_functions[x][1],
          (call_script, "script_update_formation_order_panel", ":player_team"),
        (else_try),
          ] for x in range(0, len(formation_order_functions))
          ])+[
          (eq, ":object", "$g_battle_map_plane"),
          (get_scene_boundaries, pos2, pos3),
          (mouse_get_position, pos1),
          (set_fixed_point_multiplier, 1000),
          (position_get_x, ":mouse_x", pos1),
          (position_get_y, ":mouse_y", pos1),
          (store_sub, ":map_x", 980, "$g_battle_map_width"),
          (store_sub, ":map_y", 730, "$g_battle_map_height"),
          (val_sub, ":mouse_x", ":map_x"),
          (val_sub, ":mouse_y", ":map_y"),
          (val_clamp, ":mouse_x", 0, "$g_battle_map_width"),
          (val_clamp, ":mouse_y", 0, "$g_battle_map_height"),
          (val_mul, ":mouse_x", "$g_battle_map_scale"),
          (val_mul, ":mouse_y", "$g_battle_map_scale"),
          (set_fixed_point_multiplier, 1000),
          (position_set_x, pos1, ":mouse_x"),
          (position_set_y, pos1, ":mouse_y"),
          (position_transform_position_to_parent, pos3, pos2, pos1),
          (copy_position, pos1, pos3),
          (get_player_agent_no, ":player"),
          (agent_get_position, pos3, ":player"),
          (position_copy_rotation, pos1, pos3),
          (call_script, "script_formation_move_to_pos_key_reaction"),
          (call_script, "script_update_formation_order_flags_on_map"),
          (call_script, "script_update_formation_order_panel", ":player_team"),
        (try_end),
        ]),
      (ti_on_presentation_mouse_enter_leave,
       [(store_trigger_param_1, ":object"),
        (store_trigger_param_2, ":enter_leave"),
        (try_begin),
        ]+concatenate_scripts([
          [
          (eq, ":object", "$g_presentation_obj_"+str(x+1)),
          (try_begin),
            (eq, ":enter_leave", 0),
            (overlay_animate_to_color, "$g_presentation_array_formation_"+str(x+1), 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_array_movement_"+str(x+1), 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_array_density_"+str(x+1), 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_array_facing_"+str(x+1), 250, 0xFFFFFF),
            (overlay_animate_to_color, "$g_presentation_obj_"+str(x+1+formation_arrays*2), 250, 0xFFFFFF),
          (else_try),
            (overlay_animate_to_color, "$g_presentation_array_formation_"+str(x+1), 250, 0),
            (overlay_animate_to_color, "$g_presentation_array_movement_"+str(x+1), 250, 0),
            (overlay_animate_to_color, "$g_presentation_array_density_"+str(x+1), 250, 0),
            (overlay_animate_to_color, "$g_presentation_array_facing_"+str(x+1), 250, 0),
            (overlay_animate_to_color, "$g_presentation_obj_"+str(x+1+formation_arrays*2), 250, 0),
          (try_end),
        (else_try),
          ] for x in range(0, formation_arrays)
          ])+[
        (try_end),
        ]),
      (ti_on_presentation_run,
       [(store_trigger_param_1, ":cur_time"),
        (try_begin),
        ]+[
          (this_or_next|key_clicked, formation_keys_list[x]) \
              for x in range(0, formation_arrays)
          ]+[
          (eq, 0, 1),
          (call_script, "script_update_formation_order_panel_checked_arrays"),
        (try_end),
        (try_begin),
          ]+[
          (this_or_next|key_clicked, formation_keys_list[x]) \
              for x in range(formation_arrays, len(formation_keys_list)-1)
          ]+[
          (eq, 0, 1),
          (get_player_agent_no, ":player_agent"),
          (agent_get_team, ":player_team", ":player_agent"),
          (call_script, "script_update_formation_order_panel", ":player_team"),
        (try_end),
        (try_begin),
          (gt, ":cur_time", 200),
          (key_clicked, "$key_formation_view_orders"),
          (assign, "$g_presentation_battle_formations_active", 0),
          (try_for_agents, ":cur_agent"),
            (agent_set_slot, ":cur_agent", slot_agent_map_overlay_id, 0),
          (try_end),
          (presentation_set_duration, 0),
        (try_end),
        (try_begin),
          (eq, "$formation_vector_active", 0),
          (assign, "$g_presentation_battle_formations_active", 0),
          (try_for_agents, ":cur_agent"),
            (agent_set_slot, ":cur_agent", slot_agent_map_overlay_id, 0),
          (try_end),
          (presentation_set_duration, 0),
        (try_end),
        ]),
      ]),

  
  #
  # Form Ranks Kit section end
  ##############################
  
]

